Domanda

In my view, i have three different tables. For first two tables, i have a fix height for each cell(e.g. 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?

È stato utile?

Soluzione

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

Altri suggerimenti

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top