在uitableviews属性中设置特定的uibutton

时间:2013-06-27 23:29:40

标签: ios objective-c uitableview

我试图将uitableview单元格中的特定按钮设置为非活动状态并更改其他一些属性。我从NSUserdefaults检索一个bool工作正常。这就是我试图这样做的方式。 - PopularObjectID是一个带有一串字符串的可变数组。此代码在此方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

以下是代码:

NSString *objID = [NSString stringWithFormat:@"%@",[popularObjectIDs objectAtIndex:indexPath.row]];

if(![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%@OBJPressed",objID]]) {}
else {
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];




[(UIButton *)[thisCell.contentView viewWithTag:6] setBackgroundColor:[UIColor grayColor]];
[(UIButton *)[thisCell.contentView viewWithTag:9] setBackgroundColor:[UIColor grayColor]];
[(UIButton *)[thisCell.contentView viewWithTag:6] setEnabled:NO];
[(UIButton *)[thisCell.contentView viewWithTag:9] setEnabled:NO];
}

我想我可能在这里犯了一个非常愚蠢的错误,但我对UITableViews并不是很好。 希望这是可以理解的,但如果没有;不要犹豫。

谢谢! (请好,我是新手)

3 个答案:

答案 0 :(得分:0)

无需在单元格上调用contentView。就这样做

UIButton *button = (UIButton *)[thisCell viewWithTag:6];
[button setBackgroundColor:[UIColor grayColor]];

答案 1 :(得分:0)

你应该这样做:

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
    static NSString* simpleTableIdentifier = @"TSTableViewCell";

    TSTableViewCell* cell = (TSTableViewCell*)[tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];

    if (cell == nil)
    {
        // Create TSTableViewCell.xib with UITableViewCell (as TSTableViewCell)
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed: simpleTableIdentifier
                                                     owner: self
                                                   options: nil];
        cell = [nib objectAtIndex: 0];
    }

    NSString* objID = [NSString stringWithFormat:@"%@",[popularObjectIDs objectAtIndex:indexPath.row]];

    if([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%@OBJPressed",objID]])
    {
       [cell.button6 setBackgroundColor: [UIColor grayColor]];
       [cell.button6 setEnabled: NO];
       [cell.button9 setBackgroundColor: [UIColor grayColor]];
       [cell.button9 setEnabled: NO];
    }
    else
    {
        // Set up button's property by default
    }

    return cell;
}

答案 2 :(得分:0)

如果您将该代码放入中 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 你不能使用
获得一个单元格 UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; 此时此indexPath处的单元格尚未创建。所以[tableView cellForRowAtIndexPath:indexPath]只会返回零。 你应该做的是:

static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSString *objID = [NSString stringWithFormat:@"%@",[popularObjectIDs objectAtIndex:indexPath.row]];

if(![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%@OBJPressed",objID]]) {}
else {
    [(UIButton *)[cell.contentView viewWithTag:6] setBackgroundColor:[UIColor grayColor]];
    [(UIButton *)[cell.contentView viewWithTag:9] setBackgroundColor:[UIColor grayColor]];
    [(UIButton *)[cell.contentView viewWithTag:6] setEnabled:NO];
    [(UIButton *)[cell.contentView viewWithTag:9] setEnabled:NO];
}
return cell;