2013-07-08 34 views
1

的tablview,我该怎么办?我想做一个chat.so的演示,当我打开它的UITableView时,它应该显示从上一个和上一个项目应该在上侧。即,我想在用户打开聊天屏幕时自动将我的UITableView滚动到最后一行。我该怎么办?如果我想要看最后一个

我的代码是 -

cell = [tv dequeueReusableCellWithIdentifier:@"Cell"]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease]; 
} 

    [lblName setFont:[UIFont fontWithName:@"Arial" size:12]]; 
    lblName.lineBreakMode = NSLineBreakByWordWrapping; 
    lblName.numberOfLines = 0; 
    lblName.text=[arrUsername objectAtIndex:indexPath.row]; 
    lblName.textColor = [UIColor blackColor]; 
    [lblName setBackgroundColor:[UIColor grayColor]]; 
    if ([[arrCommentUid objectAtIndex:indexPath.row] isEqualToString:@"1"]) { 
     [lblName setTextAlignment:NSTextAlignmentLeft]; 
    } 
    else 

请帮助!它的代码只是optional.cell的UITableView

回答

0

为了做到这一点,你应该按降序排列你的数组,这样它会显示你的数据从最后到第一。

对于您可以使用NSSortDescriptor

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)]; 
NSArray* sortedArray = [<YOURS_ARRAY> sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

欣赏节目!

-1

您可以滚动持续使用scrollToRowAtIndexPath

[tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

,并且可以使用

得到最后一排的indexPath

NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:([yourTableAry count] - 1) inSection:0]。

或者

NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:([tableView numberOfRowsInSection:0] - 1) inSection:0]。

编辑

如果你想行返回顶部则

NSIndexPath *firstRowIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

[tableView scrollToRowAtIndexPath:firstRowIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
+0

这里我写这篇文章的代码? – Jeet

+0

但在使用这段代码后,我无法走上正轨。我该怎么办? – Jeet

+0

帮助我亲爱的..... – Jeet

相关问题