Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 different types of custom UITableViewCells in UITableView

in my UITableView i want to set for the first news of an rss feed a custom tableViewCell(Type A lets say) and for the other news second, third etc.. another custom tableViewCell(trype B) the problem is that the custom tableViewCell(trype A) created for the first news is reused, but curiously the number of rows between the first use of the customViewCell(type A) and the second appearance of the same type of customViewCell is not equal..

my cellForRowAtIndexPath it looks like this.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{      int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];     Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];     static NSString *CellIdentifier = @"Cell";      if(feedIndex == 0){         MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];          if (cell == nil)         {             cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];             [[[cell subviews] objectAtIndex:0] setTag:111];         }          cell.feed = item;          return cell;      }     else{         NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];          if (cell == nil)         {             cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease];             [[[cell subviews] objectAtIndex:0] setTag:111];         }          cell.feed = item;          return cell;      }     return nil; }     

the the two types of cells have different heights which is set correctly. could someone point me in the right direction on how to make the type A custom cell to appear only for the first news(not being reused)? thank you

like image 987
Sorin Antohi Avatar asked Sep 10 '09 14:09

Sorin Antohi


People also ask

What is a UITableView?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.

What is tableView cell?

A table view tracks the height of rows separately from the cells that represent them. UITableView provides default sizes for rows, but you can override the default height by assigning a custom value to the table view's rowHeight property. Always use this property when the height of all of your rows is the same.

What is reuse identifier in Xcode?

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in init(frame:reuseIdentifier:) and cannot be changed thereafter.

Is it possible to add UITableView within a UITableViewCell?

yes it is possible, I added the UITableVIew within the UITableView cell .. :) no need to add tableview cell in xib file - just subclass the UITableviewCell and use the code below, a cell will be created programatically.


1 Answers

You should create a different cell identifier for the two styles of cell:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1]; Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];  static NSString *CellIdentifier1 = @"Cell1"; static NSString *CellIdentifier2 = @"Cell2";  if(feedIndex == 0) {     MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];     if (cell == nil) {        cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];        [[[cell subviews] objectAtIndex:0] setTag:111];    }     cell.feed = item;     return cell; } else {    NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];     if (cell == nil) {        cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];        [[[cell subviews] objectAtIndex:0] setTag:111];    }     cell.feed = item;     return cell; }  return nil; } 
like image 115
Philippe Leybaert Avatar answered Sep 22 '22 23:09

Philippe Leybaert