0

I am calculating height of Cell programatically in heightForRowAtIndexPath-

My complete method is:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];

    if([cell isKindOfClass:[Custom class]])
    {
        return 100;
    }
    else
    {
         return 20;
    }
}

My app takes 3-5 sec to Show the ViewController. Although cellForRowAtIndexPath is called and I can show the logs in Console.

But My ViewController not loaded.

When just return the height using this code app works fine:

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

    return 100;
}

I don't know that is the issue with this line:

id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];

What I am missing in this issue?

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • see this http://stackoverflow.com/questions/18206448/how-can-i-get-the-height-of-a-specific-row-in-my-uitableview and http://stackoverflow.com/questions/13968316/get-uitableviewcells-height-after-setting-height-for-each-uitableviewcell – Anbu.Karthik Aug 05 '16 at 08:55
  • @Anbu.Karthik I need to find the type of cell as I have two type of cell. that's the main issue. Whenever i use the code to find the type of cell my app start making delays – Rahul Aug 05 '16 at 08:58
  • 2
    Why don't you use a property isTableViewWithCustomCells or isTableViewWithOtherCustomCells? It should be faster. and I think that `heightForRowAtIndexPath:` may be called before cellForRowAtIndexPath and the dequeue stuff. – Larme Aug 05 '16 at 09:00
  • see this http://stackoverflow.com/questions/5254723/how-to-obtain-the-uitableviewcell-within-heightforrowatindexpath – Anbu.Karthik Aug 05 '16 at 09:02
  • some times `heightForRowAtIndexPath` call before cellForRowAtIndexPath then what? – Prashant Tukadiya Aug 05 '16 at 09:02
  • please post code for cell for row at indexpath – Prashant Tukadiya Aug 05 '16 at 09:03
  • @Larme Thanks for your approach. Why haven't I thought this way . – Rahul Aug 05 '16 at 09:10
  • Please use UITableviewCell instead of id – Jayesh Mardiya Aug 05 '16 at 11:22
  • What is name of the Two Type of Custom Cell? – user3182143 Aug 05 '16 at 12:10
  • well I can't see where you are getting height of cell heightForRowAtIndexPath will setup height for row in tableview. – Saad Chaudhry Aug 05 '16 at 12:59
  • and why you are setting fixed height for cells, if you want to change height give it dynamic height instead. Calculate height and assign to heightForRowAtIndexPath. – Saad Chaudhry Aug 05 '16 at 13:05
  • see answer over here. http://stackoverflow.com/questions/38785870/set-dynamic-width-and-height-of-collection-view-cell/38789275#38789275 – Saad Chaudhry Aug 05 '16 at 13:06

2 Answers2

0

You have some logic in the cellForRowAtIndexPath: method that determines what type of cell that row should be. Add that logic into the heightForRowAtIndexPath: method to determine the what the height of each row should be.

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
0

Your this line is wrong

id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];

it will force your cell to reuse.

Use

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id cell=[tableView cellForRowAtIndexPath:indexPath];

    if([cell isKindOfClass:[CustomCellClass class]])
    {
        return 100;
    }
    else
    {
         return 20;
    }
}
Yogesh More
  • 239
  • 3
  • 4