Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate UITableView from the bottom upwards?

is it possible to reverse the order of a tableView. I have searched a lot for a solution but all the results have not quite been a solution to what I am trying to achieve. They all suggest scrolling to the last position of a table with scrollToRowAtIndexPath and populating the data in reverse. But this doesn't work if the table content is dynamic and in some instances not all the cells have data. For example in a normal tableView the order is:


label 1

label 2

label 3

empty

empty

scroll direction
v
V


the desired result would be:
scroll direction
^
^

empty

empty

empty

label 3

label 2

label 1


in this example if I used the suggested method of scrollToRowAtIndexPath and use the length of the array of objects, I would only get the third cell from the top. And end up with something like this:


unwanted outcome:

label 3

label 2

label 1

empty

empty

scroll direction
v
V


any help would be great thank you.

like image 853
kai Taylor Avatar asked Jan 23 '15 08:01

kai Taylor


People also ask

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

How do I scroll to the top of a Tableview table?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.


3 Answers

To populate UITableView from the bottom:

- (void)updateTableContentInset {     NSInteger numRows = [self.tableView numberOfRowsInSection:0];     CGFloat contentInsetTop = self.tableView.bounds.size.height;     for (NSInteger i = 0; i < numRows; i++) {         contentInsetTop -= [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];         if (contentInsetTop <= 0) {             contentInsetTop = 0;             break;         }     }     self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0); } 

To reverse the order of elements:

dataSourceArray = dataSourceArray.reverseObjectEnumerator.allObjects; 

Swift 4.2/5 version:

func updateTableContentInset() {     let numRows = self.tableView.numberOfRows(inSection: 0)     var contentInsetTop = self.tableView.bounds.size.height     for i in 0..<numRows {         let rowRect = self.tableView.rectForRow(at: IndexPath(item: i, section: 0))         contentInsetTop -= rowRect.size.height         if contentInsetTop <= 0 {             contentInsetTop = 0             break         }     }     self.tableView.contentInset = UIEdgeInsets(top: contentInsetTop,left: 0,bottom: 0,right: 0) } 

Swift 3/4.0 version:

self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0) 
like image 113
KlimczakM Avatar answered Oct 09 '22 00:10

KlimczakM


first reverse uitableview

    tableView.transform = CGAffineTransformMakeScale (1,-1); 

then reverse cell in cell create.

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;      ...      cell.contentView.transform = CGAffineTransformMakeScale (1,-1); 
like image 28
waynett Avatar answered Oct 09 '22 01:10

waynett


Swift 4.0 and 4.2 version

First reverse UITableView in viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
}

Then reverse the cell in cellForRowAt.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as? MyTableViewCell else { fatalError() }

        cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
        return cell
    }
like image 29
Akbar Khan Avatar answered Oct 08 '22 23:10

Akbar Khan