简体   繁体   中英

Height of cells in different tableViews in iOS

In my view, i have three different tables. For first two tables, i have a fix height for each cell(eg say 30.0f for each cell of first table and 45.0f for each cell of second table) which i have adjusted in the storyboard according to the data in each table. For the third table, i want to have a different height for each cell. I tried the method

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

But the issue with this technique is that this method is called for each table. As for the first two tables, i have adjusted the height in storyboard, i don't know what to do with them.And if i do:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == myTable) {
    return 150.0;
}
}

As i have nothing to return for the else cases, the whole thing fails. Any suggestion?

You need to return a height for the other tables.

If you have set there height in the storyboard you can access that height like so:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == myTable)
        return 150.0;

    return tableView.rowHeight;
}

tableView.rowHeight will be the height that you set in the storyboard

You should make your view controller delegate of all 3 tableviews and in heightForRowAtIndexPath check which table called the method:

So in viewDidLoad add:

myTable1.delegate = self;
myTable2.delegate = self;
myTable3.delegate = self;

and then:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath     *)indexPath {
if(tableView == myTable1) {
return 150.0;
}
else if(tableView == myTable2) {
return 75.0;
}
else if(tableView == myTable3) {
return 75.0;
}

}

How about using the tag property? Set the tag for each table to a unique number then return height like this:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == 1)
    {
        return 150;
    }
    return 250;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM