我有一个包含三行的tableView,现在我想将cell2移动到tableView的顶部,以便Cell1变得完全不可见(超出tableView的边界)
这是我尝试过的:
func textViewDidBeginEditing(textView: UITextView) {
let index = NSIndexPath(forItem: 1, inSection: 0)
tableView.scrollToRowAtIndexPath(index, atScrollPosition: .Top, animated: true)
}
上面什么也没做,也许我错过了一些东西
我也尝试过这个:
func textViewDidBeginEditing(textView: UITextView) {
let cgpoint = CGPointMake(0, (CGFloat(-64 - (channelCellRowHeight )))) // channelCellRowHeight is the hight of my row which i want move outside the tableview so that cell2 can stay at top of tableview
// here cgpoint is not accurate
tableView.setContentOffset(cgpoint, animated: true)
}
我想知道一定有更简单的方法来做到这一点
scrollRowToTop
API
有什么线索我怎样才能实现我想要的吗?
最佳答案
如果您的表格 View 单元格具有不同的单元格高度,那么您需要实现此委托(delegate)方法,以便表格 View 可以计算出准确的单元格高度值,以便能够将单元格滚动到您想要的位置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText;
cellText = [dataArray objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:@
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height;
}
希望这个回答对你有帮助
关于ios - 将 UITableViewCell 滚动到 tableview 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39056089/