我在故事板上创建了一个表视图控制器。我想在单击所选行时将UILabel文本颜色更改为绿色。
我正在尝试这样的事情,但它不起作用:
- (void)viewDidLoad {
[super viewDidLoad];
menuItems = @[@"home", @"stamp", @"scanner", @"settings"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
NSLog(@"cell %@",[cell.contentView viewWithTag:1000]);
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0){
UILabel *menu= (UILabel*)[cell.contentView viewWithTag:1000];
menu.textColor = [UIColor greenColor];
NSLog(@"cell clicked: %@",[cell.contentView viewWithTag:1000]);
}
//[cell.textLabel setTextColor:[UIColor greenColor]];
// [self setCellColor:[UIColor greenColor] ForCell:cell];
[self.tableView reloadData];
}
我在表格单元格中拖动标签并将标识符设置为home,stamp,scanner ...并将标记更改为1000.
有谁能告诉我为什么标签文字颜色仍然保持不变并为我提供解决方案?
答案 0 :(得分:0)
在didSelectRowAtIndexPath
UITableViewCell *cell=(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath.row];
cell.(whatever your label property).textColor=[UIColor greenColor];
这将改变所选单元格上标签的颜色。
答案 1 :(得分:0)
如果单元格中有多个标签,则需要单独设置其颜色 要获取加载到内存中的单元格,请使用
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
删除if(indexPath.row == 0)
条件以在每个单元格上应用颜色。
并在cell.contentView中循环标签
(如果是多个标签,您也可以通过标签获取,但是对每个标签应用唯一标签并获得带标签的每个标签)
for (id thisLabel in cell.contentView.subviews) {
if ([thisLabel isKindOfClass:[UILabel class]]) {
UILabel *currentlabel = thisLabel;
[currentlabel setTextColor:[UIColor greenColor]];
}
}
对于单个标签,上面的循环可以使用,但更容易使用标记
UILabel *currentLabel= (UILabel*)[cell.contentView viewWithTag:1000];
[currentLabel setTextColor:[UIColor greenColor]];
答案 2 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (selectedRow == 0) {
UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
[homeLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
}else if (selectedRow == 3){
UILabel *settingLabel= (UILabel*)[cell.contentView viewWithTag:1400];
[settingLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
}
if(selectedRow!= indexPath.row){
UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
[homeLabel setTextColor:[UIColor whiteColor]];
UILabel *stampLabel= (UILabel*)[cell.contentView viewWithTag:1200];
[stampLabel setTextColor:[UIColor whiteColor]];
UILabel *scannerLabel= (UILabel*)[cell.contentView viewWithTag:1300];
[scannerLabel setTextColor:[UIColor whiteColor]];
UILabel *settingLabel= (UILabel*)[cell.contentView viewWithTag:1400];
[settingLabel setTextColor:[UIColor whiteColor]];
}
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedRow = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 0){
UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
[homeLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
[self.tableView reloadData];
}
if(indexPath.row == 1){
UILabel *stampLabel= (UILabel*)[cell.contentView viewWithTag:1200];
[stampLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
[self.tableView reloadData];
}
if(indexPath.row == 2){
UILabel *scannerLabel= (UILabel*)[cell.contentView viewWithTag:1300];
[scannerLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
[self.tableView reloadData];
}
if(indexPath.row == 3){
UILabel *settingLabel = (UILabel*)[cell.contentView viewWithTag:1400];
[settingLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
[self.tableView reloadData];
}
}