如何在UITableView中拥有更多行的同时使特定的UITableViewCell在屏幕上可见

时间:2012-02-06 09:25:41

标签: objective-c ios uitableview

我在表格视图中有20行,我在屏幕的一半(使用IB)设计(或说调整大小)UITableView,在半屏幕中我显示与特定UITableViewCell相关的内容。将在半屏幕中显示哪个单元格的详细信息是运行时间。我希望在加载第二个最后一个单元格的视图时可以看到单元格。

我怎样才能做到这一点?

6 个答案:

答案 0 :(得分:18)

我发现确保整个单元格可见的最简单方法是让tableview确保单元格的rect是可见的:

[tableView scrollRectToVisible:[tableView rectForRowAtIndexPath:indexPath] animated:YES];

测试它的一种简单方法就是将其放入-(void)tableView:didSelectRowAtIndexPath: - 如果它被部分隐藏,则点击任何单元格然后将其滚动到位。

答案 1 :(得分:12)

UITableView课程提供:

-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
             atScrollPosition:(UITableViewScrollPosition)scrollPosition
                     animated:(BOOL)animated;

这似乎是你正在寻找的。

NSIndexPath对象可以使用:

构建
+(NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;

答案 2 :(得分:7)

尝试scrollToRowAtIndexPath:atScrollPosition:animated:

NSUInteger indexArray[] = {1,15};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:2];
[yourTableView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionTop animated:YES];

答案 3 :(得分:3)

Swift 4 版本的Nico teWinkel的回答,对我来说效果最好:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.scrollToRow(at: indexPath, at: .middle, animated: true) //scrolls cell to middle of screen
}

答案 4 :(得分:0)

Nip teWinkel的

Swift 5 版本使用了UITableview的 scroll rect to visible 方法。

MERGE (species:Reference:Species{ GUID: $reference.guid })
MERGE (image:Image:Media{GUID: $blobFile.imageGuid})
ON CREATE SET
    image.GUID = apoc.create.uuid(),
    image.creationDate = datetime(),
    image.name = species.name
ON MATCH SET
    image.name = species.name
MERGE (species)<-[r6:IS_ABOUT]-(image)
WITH species, image WHERE $blobFile.extension IS NOT NULL
SET 
    image.filename = $blobFile.filename, 
    image.url = 'https://www....' + image.GUID + '.' + $blobFile.extension

答案 5 :(得分:0)

像这样使用

let indexPath = IndexPath(row:row, section:section)
self.tableView.scrollToRow(at:indexPath, at:.top, animated:true)