2

When i add some text in TableView, it will show from Top of the tableview but i want to show it from bottom of tableView.

1. My TableView Screen

enter image description here

When i add user entered text in array and refresh tableView it will load from Top of TableView like below image.

enter image description here

but i want to add it from bottom like below image.

enter image description here

Can you guys help me, how can i achieve this?

Thanks

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • Also specify language. – Sakir Sherasiya May 04 '17 at 11:14
  • My Answer is correct or not I don't know. But you can achieve your requirement. First took an uiview and set border as screen. Add table view at bottom of UIView. Next set table view height as cell height. If you enter text second time resize the table view height and set text at 2nd index in an array. – phani May 04 '17 at 11:22
  • i think during adding an object to an array which reloads the tableview just add objects using "InsertObjectAtIndex" method ,and add object always at index 0 and reload tableview.... – Zღk May 04 '17 at 11:29
  • @Zck, thanks for your reply. i already try with insertObjectAt 0 Index but i will bind data from topOf TableView. – Kuldeep May 04 '17 at 11:36
  • did you try using ... - (void) insertRowsAtIndexPaths:(NSArray*) indexPaths withRowAnimation: (UITableViewRowAnimation) animation – Zღk May 04 '17 at 11:58
  • @Zck, yes i tried with insertRowsAtIndexPaths like that : [self.arrPublicCommets insertObject:self.txtVWComment.text atIndex:0]; NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tblVWComments insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationBottom]; But still it will add it from Top of TableView – Kuldeep May 04 '17 at 12:12
  • 1
    Try this http://stackoverflow.com/a/5688793/7250862 – RajeshKumar R May 04 '17 at 12:21
  • @RajeshkumarR, Thanks a lot, It's working – Kuldeep May 04 '17 at 12:32
  • Post it as an answer. It will be useful for someone else. – RajeshKumar R May 04 '17 at 12:33

2 Answers2

3

I am able to bind Data from bottom of TableView. All credit goes to Rajeshkumar R for helping me.

Step 1, apply a transform to the table view rotating it 180deg

tableView.transform = CGAffineTransformMakeRotation(-M_PI);

Step 2, rotate your raw cell 180deg in tableView:cellForRowAtIndexPath:

cell.transform = CGAffineTransformMakeRotation(M_PI);

Step 3, reverse your datasource. If you're using an NSMutableArray insert new objects at location 0 instead of using AddObject... Now, the hard part is remembering that left is right and right is left only at the table level, so if you use

[tableView insertRowsAtIndexPaths:targetPath withRowAnimation:UITableViewRowAnimationLeft]
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
0

you can use the function below. It worked for me.

func scrollToLastRow() {
    let indexPath = NSIndexPath(row: messageHistoryArray.count-1, section: 0)
    self.createChatTableView.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
}
Stephanie
  • 121
  • 2
  • 12