UITableViewCell with UITableViewCellStyleValue1 by adding a new row in detailTextLabel to the cell below

In my view of the table, I have the last cell, which is not initially visible, as seen in the first image, when I look up the list, in the second image you can see that the price or my detailTextLabel is placed on a new line, and not supporting the correct justification.
image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.jpg

image 2 http://img706.imageshack.us/img706/6007/iphoneerror2edited.jpg


Here is the code, I can’t understand why it does it, any direction or help will be highly appreciated

- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell. NSUInteger indexRow = [indexPath row]; switch (indexRow) { case 0:{ NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet]; NSString *description = [[currentData objectForKey:@"Description"] stringByTrimmingCharactersInSet:set]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryNone; cellShift = 1; if (![description isEqualToString:@""]) { cell.textLabel.text = @""; cell.detailTextLabel.text = description; cell.detailTextLabel.numberOfLines = 2; } else { cell.textLabel.text = @""; cell.detailTextLabel.text = @""; cell.detailTextLabel.numberOfLines = 0; } break; } default:{ if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; } NSDictionary *item = [tableData objectAtIndex:(indexRow-cellShift)]; NSString *name = [item objectForKey:@"Name"]; if ([name length] > MaxVendorsLength ) { name = [NSString stringWithFormat:@"%@ ...",[name substringToIndex:MaxVendorsLength]]; } cell.textLabel.text = name; cell.textLabel.minimumFontSize = 12; NSString *priceString; float price = [[item objectForKey:@"Price"] floatValue]; //NSLog(@"| %@ | : | %@ |",[item objectForKey:@"Name"], [item objectForKey:@"Price"]); if (price != 0) { priceString = [[NSString alloc] initWithFormat:@"$%.2f",price]; } else { priceString = [[NSString alloc] initWithString:@"--"]; } cell.detailTextLabel.text = priceString; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; [priceString release]; break; } } cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; cell.textLabel.minimumFontSize = 14; cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:15]; cell.detailTextLabel.minimumFontSize = 14; return cell; } 

Let me know if I need to post anything else to get help on this.

+4
source share
3 answers

This is because when you scroll up, the top cell is reused, because all your cells have the same cell ID (you have the first row). You need to declare the identifiers of two cells and use them depending on which row you are trying to get.

 static NSString *FirstRowCellIdentifier = @"A"; static NSString *OtherRowCellIdentifier = @"B"; NSString *cellIdentifier = nil; if ([indexPath row] == 0) cellIdentifier = FirstRowCellIdentifier; else cellIdentifer = OtherRowCellIdentifier; UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:cellIdentifier]; // ..... 

Then you can use the rest of your code as is. This simply ensures that the reused cell is of the correct type.

+9
source

You use the same cell id for all rows, but the 0th row has a different style. When scrolling, a subtitle style cell can be reused.

Try using a different cell id for the 0th row. Save only the cell declaration outside the switch and move the dequeueReusableCellWithIdentifier in each case.

+2
source
  (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
+1
source

Source: https://habr.com/ru/post/1311264/


All Articles