2016-08-20 32 views
0

我有顶部的tableView有三排,现在我想我的小区2移动到的tableView的顶部,这样小区1变得完全看不见(出的tableView边界的)滚动一个UITableViewCell的实现代码如下

这是我的尝试:

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

任何线索我如何能实现我想要什么?

+0

你需要计算你想要移动到顶部的所有单元格的高度,并将它们设置为你的contentOffset的Y位置 –

+0

你找到了解决方案吗? –

+0

man我在计算我的行高时遇到了问题,这就是我在做的:'let frame = tableView.rectForRowAtIndexPath(NSIndexPath(forRow:0,inSection:0)) defaultRowHeight = frame.height' @MohammedShakeer但那么该怎么做?导致我的默认contentOffset是'(0.0,-64.0)'(当tableView行在他们的默认位置) –

回答

1

,如果你的表视图细胞有不同的单元格的高度,那么你需要实现这个委托方法使表视图可以计算出一个准确的单元格高度值,以便能够滚动细胞您想要的位置

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

希望这个答案可以帮助你