我想在 UITableViewCell 的 initWithStyle 方法中更改 UILabel 文本颜色。但是颜色并没有改变。但是当在 cellForRowAtIndexPath 方法中完成颜色更改时,颜色会发生变化。为什么?
			
			1465 次
		
1 回答
            0        
        
		
根据您使用标签的方式,您只需要设置 textColor 属性:
为了在方法中创建 tableViewCell,- (UITableViewCell*)tableView:(UITableView *)tableView我认为您使用了如下方法:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
UITableViewCellStyleDefault您的样式可能是UITableViewCellStyleValue1 orUITableViewCellStyleValue2 orUITableViewCellStyleSubtitle`。
现在,如果您添加自己的标签而不是使用,则无论您使用什么样式:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
lbl.text = @"your Text";
[lbl setTextColor:[UIColor greenColor]];
如果您没有添加自己的标签并使用对象textLabel或对象detailTextLabel的默认标签UITableViewCell:
[cell.textLabel setTextColor:[UIColor greenColor]];
[cell.detailTextLabel setTextColor:[UIColor greenColor]];
如果您在子视图中添加任何标签,例如:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
lbl.text = @"your Text";
[lbl setTextColor:[UIColor greenColor]];
你可以greenColor用你想要的颜色替换。希望这可以帮助 :)
于 2012-05-15T10:20:54.023   回答