繁体   English   中英

滚动UITableViewCell到tableview的顶部

[英]scroll a UITableViewCell to top of the tableview

我现在有一个带有三行的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

任何线索,我怎么能达到我想要的?

如果您的表格视图单元格具有不同的单元格高度,则您需要实现此委托方法,以便表格视图可以计算出准确的单元格高度值,以便能够将单元格滚动到所需位置

- (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;
}

希望这个答案对您有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM