簡體   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