开发者

Change the text color of all the cells in a table view

开发者 https://www.devze.com 2023-03-26 15:16 出处:网络
Is there a way to change the color of the text in ALL of the cells in a table view? I\'ve looked this up before and it only talks about ways to change the text color of one cell. And my app does not h

Is there a way to change the color of the text in ALL of the cells in a table view? I've looked this up before and it only talks about ways to change the text color of one cell. And my app does not h开发者_开发百科ave a set number of cells, so this wouldn't work.


In:

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

Do:

cell.textLabel.textColor = [UIColor greenColor];


You can do it with the textLabel property of the UITableViewCell. But you cannot change the frame size of the textLabel.

cell.textLabel.textColor = [UIColor blackColor];

To change the frame size and stuff, you will need to create a UILabel and give it a proper frame, color and text and then add it as a SubView.

[cell.contentView addSubview: myLabel];

For this approach, make sure to take care of the reusability problems (you will need to subclassUITableViewCells and create CustomUITableViewCells).


You can do this in one of two ways:

  1. Implement a custom UITableViewCell subclass that sets the label's textColor to your desired color.

  2. Change textColor inside cellForRowAtIndexPath. Just insert the following code somewhere before return cell;

    cell.textLabel.textColor = [UIColor redColor];
    


If you want to avoid the delegate method, and change dynamically the visible rows...

Try this out:

NSEnumerator *e = [[self.tableView indexPathsForVisibleRows] objectEnumerator];
id object;
while (object = [e nextObject]) {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:object];
    cell.textLabel.textColor = [UIColor grayColor];
}

Works like a charm.

0

精彩评论

暂无评论...
验证码 换一张
取 消