更改UITableViewCell中的对象属性

时间:2011-11-15 13:45:12

标签: iphone uitableview uibutton

我有什么理由不能更改单元格内对象的属性吗?我在一个单元格中有几个按钮,当选择一个按钮时,应该取消选择另一个单元格。这工作正常,除非我在设置单元格时设置属性。例如,我在标题中设置了出口:

@interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UIActionSheetDelegate> {

    UIButton *mButton;
    UIButton *fButton;

}

然后@property (nonatomic, retain) IBOutlet UIButton *mButton;他们,并合成。如果我使用mButton.selected = YES;切换所选状态,但是如果我在创建单元格时设置了默认值(即将其中一个按钮设置为选中),则拒绝让我切换选定状态。它始终保持选中状态。

我也尝试使用UIImageViews,使用按钮切换其alpha属性,但如果我在创建单元格期间设置alpha属性,它将永远不会从该状态更改。

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // ... some text label stuff deleted here.

    cell.opaque = NO;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

}

if (indexPath.row == 2) {
    // Gender
    cell.textLabel.text = @"Gender";
    UIImageView *tmpImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UICellsBottom.png"]];
    cell.backgroundView = tmpImage;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    // Male button
    mButton = [UIButton buttonWithType: UIButtonTypeCustom];
    mButton.frame = CGRectMake(200, 3, 45, 45);
    mButton.adjustsImageWhenHighlighted = NO;
    [mButton setBackgroundImage: [UIImage imageNamed:@"MF-Male-Off@2x.png"] forState: UIControlStateNormal];
    [mButton setBackgroundImage: [UIImage imageNamed:@"MF-Male-On@2x.png"] forState: UIControlStateSelected];
    [mButton addTarget:self action:@selector(male) forControlEvents: UIControlEventTouchUpInside];
    [cell addSubview: mButton];

    mButton.selected = YES;

    // Female button
    fButton = [UIButton buttonWithType: UIButtonTypeCustom];
    fButton.frame = CGRectMake(254, 3, 45, 45);
    fButton.adjustsImageWhenHighlighted = NO;
    [fButton setBackgroundImage: [UIImage imageNamed:@"MF-Female-Off@2x.png"] forState: UIControlStateNormal];
    [fButton setBackgroundImage: [UIImage imageNamed:@"MF-Female-On@2x.png"] forState: UIControlStateSelected];
    [fButton addTarget:self action:@selector(female) forControlEvents: UIControlEventTouchUpInside];
    [cell addSubview: fButton];

}

return cell;
}

然后我的按钮操作如下:

- (void) male
{
    gender = @"m";
    mButton.selected = YES;
    fButton.selected = NO;
}

1 个答案:

答案 0 :(得分:2)

而不是

 mButton.selected = YES;

使用

[mButton setHighlighted:YES];