Ilario
Ilario

Reputation: 6079

change the appearance of a single UITableViewCell based on the content

I have a problem with a UITableView with custom UITableViewCell. The table is filled by an NSArray, and I want that if an object in this NSArray begins with - changes its appearance.

the problem is that the UITableViewCell that begins with - is changed, but also change other cells that should not change.

this is my code:

  //this is the way in which change the height of the cell if the object in the array begins with -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *try = [arrayTitleEs objectAtIndex:indexPath.row];

if ([[try substringToIndex:1]isEqualToString:@"-"]) {

    return 45;

}

else return 160;
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

  static NSString *CellIdentifier = @"CellCardioScheda";
  CardioSchedaCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

   cell.titleEs.text = [arrayTitoloEs objectAtIndex:indexPath.row];

  NSString *try = [arrayTitoloEs objectAtIndex:indexPath.row];

if ([[try substringToIndex:1]isEqualToString:@"-"]) {

    cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
}


 return cell;
}

as you can see from the picture that begins with the cell - is smallest and the text is moved to the left, in the next cell is all right, but in the last cell text spostasto but it should not!

as you can see from the picture that begins with the cell - is smallest and the text is moved to the left, in the next cell is all right, but the text in the last cell is moved, but should not!

thanks to all

Upvotes: 1

Views: 125

Answers (2)

rmaddy
rmaddy

Reputation: 318784

The problem is with the following:

if ([[try substringToIndex:1]isEqualToString:@"-"]) {
    cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
}

You need an else to set the frame properly if the condition isn't met. Cells get reused. Anything you do to a cell must be done for all cells.

if ([[try substringToIndex:1]isEqualToString:@"-"]) {
    cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
} else {
    cell.titleEs.frame = CGRectMake(...); // whatever regular cells should be
}

BTW - you could replace [[try substringToIndex:1]isEqualToString:@"-"] with [try hasPrefix:@"-"].

Upvotes: 0

Giuseppe Garassino
Giuseppe Garassino

Reputation: 2292

In cellForRowAtIndexPath you can create two different types of cells and return one or the other according to your needs:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

    UITableViewCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"firstCellID"];

    if (firstCell == nil) {
        firstCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCellID"] autorelease];
    }

    // Set here firstCell

    UITableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:@"secondCellID"];

    if (secondCell == nil) {
        secondCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondCellID"] autorelease];
    }

    // Set here secondCell

    if ([[try substringToIndex:1]isEqualToString:@"-"]) {
        return secondCell;
    } else {
        return firstCell;
    }

}

Upvotes: 1

Related Questions