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

时间:2017-03-31 06:25:46

标签: objective-c

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

我的代码编写如下

- (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;

但它不起作用。请帮我。

1 个答案:

答案 0 :(得分:1)

试用这行代码

- (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
}