在UITableViewCell中更改UILabel的文本颜色

时间:2011-10-24 06:49:22

标签: iphone uitableview uilabel uicolor

我需要在按钮点击时更改UILabel中包含的UITableViewCell的文字颜色。我创建了一个UIColor object,在按钮单击时设置为指定的颜色并调用UITableView reloadData方法,即使这样颜色也没有改变。我该如何实现呢?

3 个答案:

答案 0 :(得分:0)

我假设您要更改表格中所有行的颜色。 (如果您只想更改一行,则需要其indexPath)。

创建一个跟踪颜色状态的BOOL变量。在按钮单击例程中,适当设置此值并执行[tableView reloadData]

cellForRowAtIndexPath中,您需要检查变量并每次为两种状态设置颜色。必须双向进行,否则滚动时单元格的状态将无法预测。

答案 1 :(得分:0)

要更改UITableViewCell的文本颜色,必须实现 UITableViewDataSource 委托函数

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

在那里,你得到

的单元格

UITableViewCell *tableViewcell = [self.tableView dequeueReusableCellWithIdentifier: @"Cell"];

 if (tableViewCell == nil) 
{
    tableViewCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
}

在这里你可以改变文字的颜色。

顺便说一句:你应该接受一些你的问题的答案 - 这将提高你获得未来问题答案的机会......

答案 2 :(得分:0)

很好,当您将自定义UILabel添加到contentView时,请为UILabel设置标记。

UILabel *cl = nil;
cl = (UILabel*)[cell.contentView viewWithTag:2000];
if( !cl )
{
    cl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,30)];
    cl.tag = 2000;
    [cell.contentView addSubview:cl];
}
if( beforeButtonPressed )
cl.textColor = [UIColor blackColor];
else if ( afterButtonPressed )
cl.textColor = [UIColor blueColor];

我想上述方法可以满足您的要求。

相关问题