Reputation: 27
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
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
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