David Sundström
David Sundström

Reputation: 27

Change specific cell in dynamic tableview, Swift

I want to change a label color on a specific cell in a tableview (by code). The problem is that I want to use a dynamic cell which result in that every label in every cell will have changed they colors. Is this possible? I would really appreciate some help. This is in xcode with Swift.

Upvotes: 0

Views: 2091

Answers (2)

koen
koen

Reputation: 5729

I would change it in willDisplayCell:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

// put your logic here and then change the color as appropriate, for instance:

   let row = indexPath.row
   switch row {
      case 0: 
         cell.label.color = red // pseudo code
      case 2:
         cell.label.color = green // pseudo code
   // etc
   }
}

Upvotes: 1

Florian Mielke
Florian Mielke

Reputation: 3360

In your UITableViewDataSource you can use tableView:cellForRowAtIndexPath: to update that cell at a specific indexPath, e.g.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:YourCellIdentifier forIndexPath:indexPath]
  if ([indexPath isEqual:theSpecificIndexPath]) {
    cell.label.textColor = ...;
  }
}

Upvotes: 2

Related Questions