2016-08-20 34 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; 
} 

希望這個答案可以幫助你