侧栏单元格项目应在单击时更改其颜色

时间:2022-11-21 15:32:19

I have to change button title color and image of only one cell which is selected. suppose I have clicked side bar button from main view controller then in side bar, only home buttton should be in blue color and others should be in black. I have attached screen shot to show an example. 侧栏单元格项目应在单击时更改其颜色

我必须更改按钮标题颜色和只选择一个单元格的图像。假设我已从主视图控制器中单击侧栏按钮,然后在侧栏中,只有家庭按钮应为蓝色,其他应为黑色。我附上了屏幕截图来展示一个例子。

I have written my code as below

我编写了如下代码

- (IBAction)home:(id)sender {
titleSelected = [NSUserDefaults standardUserDefaults];
[titleSelected setObject:@"home" forKey:@"CellIdentifier"];
[titleSelected synchronize];
NSLog(@"defaultValue %@",[titleSelected stringForKey:@"CellIdentifier"]);                                         
}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row==0) {


    static NSString *simpleTableIdentifier = @"but1";

    _oneCell= (oneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (_oneCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"oneTableViewCell" owner:self options:nil];

        _oneCell = [nib objectAtIndex:0];

    }
    _oneCell.titleImage.layer.cornerRadius = _oneCell.titleImage.frame.size.width / 2;
    _oneCell.titleImage.clipsToBounds = YES;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Imagekey"] != nil) {
        NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"Imagekey"];
        _oneCell.titleImage.image = [UIImage imageWithData:imageData];
    } else {
        _oneCell.titleImage.image = [UIImage imageNamed:@"user.png"];
    }


    return _oneCell;
}
 if (indexPath.row==1) {
    static NSString *simpleTableIdentifier = @"but2";

    _oneCell= (oneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if ([[titleSelected stringForKey:@"CellIdentifier"] isEqualToString:@"home"]) {
        NSLog(@"defaultValue %@",[titleSelected stringForKey:@"CellIdentifier"]);
     [_oneCell.homeButtonpressed setTitleColor:[UIColor colorWithRed:(5/255.0) green:(103/255.0) blue:(169/255.0) alpha:1.0] forState:UIControlStateNormal];

    }
    return _oneCell;
       }

NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

return cell;

but it is not working. Please Help me.

但它不起作用。请帮帮我。

1 个解决方案

#1


1  

try this line of code

试试这行代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //Get your cell for selected row       
 cell.leftMenuItemLabel.textColor = [UIColor redColor];//Configure whatever color you want
}

#1


1  

try this line of code

试试这行代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //Get your cell for selected row       
 cell.leftMenuItemLabel.textColor = [UIColor redColor];//Configure whatever color you want
}