我有一个UITableView与不同高度的单元格,我需要知道它们何时完全可见.
目前,我循环遍历可见单元格列表中的每个单元格,以检查每次滚动视图时它是否完全可见.这是最好的方法吗?
这是我的代码:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
NSArray* cells = myTableView.visibleCells;
for (MyCustomUITableViewCell* cell in cells) {
if (cell.frame.origin.y > offset.y &&
cell.frame.origin.y + cell.frame.size.height < offset.y + bounds.size.height) {
[cell notifyCompletelyVisible];
}
else {
[cell notifyNotCompletelyVisible];
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
请注意* - (NSArray )visibleCells返回可见单元格,这些单元格完全可见且部分可见.
编辑2:
这是在结合lnafziger和Vadim Yelagin的解决方案后的修订代码:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
NSArray* cells = myTableView.visibleCells;
NSArray* indexPaths = myTableView.indexPathsForVisibleRows;
NSUInteger cellCount = [cells count]; …Run Code Online (Sandbox Code Playgroud) 我正在播放视频UITableViewCell.为此,我使用以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *VideoCellIdentifier = @"VideoCell";
NSDictionary *_response_data = [self.response objectAtIndex:indexPath.row];
VideoCustomCell *cell = (VideoCustomCell *) [tableView dequeueReusableCellWithIdentifier:VideoCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects;
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"VideoCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (VideoCustomCell *) currentObject;
cell.delegate = self;
break;
}
}
}
avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:[_response_data valueForKey:@"media_id"]]] retain];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = cell.video_player_view.layer.bounds;
avPlayerLayer.videoGravity = AVLayerVideoGravityResize;
[cell.video_player_view.layer …Run Code Online (Sandbox Code Playgroud) 我有一个包含视频URL的数组,我想在UITableviewcell完全可见时播放这些视频.
我试过这个
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
NSArray* cells = homeTabl.visibleCells;
for (HomeCell* cell in cells)
{
if (cell.frame.origin.y > offset.y &&
cell.frame.origin.y + cell.frame.size.height < offset.y + bounds.size.height)
{
NSIndexPath *path = [homeTabl indexPathForCell:cell] ;
index = path.row;
fullvisible = YES;
[homeTabl reloadData];
}
else
{
fullvisible = NO;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (fullvisible)
{
NSURL *url = [NSURL URLWithString:[[responsearray objectAtIndex:indexPath.row]valueForKey:@"feed_video"]];
cell.videoItem = [AVPlayerItem playerItemWithURL:url];
cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer]; …Run Code Online (Sandbox Code Playgroud)