1

我在情节提要上创建了一个表格视图控制器。单击所选行时,我想将 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。

谁能告诉我为什么标签文本颜色仍然保持不变并为我提供解决方案?

4

3 回答 3

0

didSelectRowAtIndexPath

 UITableViewCell *cell=(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath.row];
cell.(whatever your label property).textColor=[UIColor greenColor];

这将更改所选单元格上标签的颜色。

于 2015-05-15T04:15:52.653 回答
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]];
于 2015-05-15T04:25:51.823 回答
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];
    }


}
于 2015-05-15T06:09:18.910 回答