1

hey I want to make a demo of chat.so when I open UITableView for it, it should display from the last and previous item should be in upper side.. i.e., I want to scroll my UITableView to the last row automatically when user opens the chat screen. what should i do?

my code is-

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

please help!! its the code for just optional.cell of UITableView

Code cracker
  • 3,009
  • 6
  • 32
  • 58
Jeet
  • 13
  • 5

3 Answers3

3

try this for chat application

SSMessagesviewcontroller
stbubbletableviewcell
bubblethingie
uibubbletableview
styledchat
acanichat

and also you can go to last row using

   [tableView reloadData];
   int lastRowNumber = [tableView numberOfRowsInSection:0] - 1;
   NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
   [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
SAMIR RATHOD
  • 3,500
  • 1
  • 17
  • 44
0

In order to do this, you should sort your array in descending order.Thus it will display your data from last to first.

For that you can use NSSortDescriptor

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

Enjoy Programming !

Niru Mukund Shah
  • 4,525
  • 2
  • 18
  • 34
-1

You can scroll to last using scrollToRowAtIndexPath

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

And you can get indexPath of last row using

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

Or

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

EDIT

If you want to goto top of row then

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

[tableView scrollToRowAtIndexPath:firstRowIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Rajneesh071
  • 30,084
  • 13
  • 60
  • 74