2011-11-15 114 views
0

是否有任何理由不能更改单元内对象的属性?我有一个单元格中的几个按钮,当选择一个时,另一个单元格应该被取消选中。这工作正常,除非设置单元格时设置属性。例如,我成立了我的头网点:更改UITableViewCell中的对象属性

@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:@"[email protected]"] forState: UIControlStateNormal]; 
    [mButton setBackgroundImage: [UIImage imageNamed:@"[email protected]"] 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:@"[email protected]"] forState: UIControlStateNormal]; 
    [fButton setBackgroundImage: [UIImage imageNamed:@"[email protected]"] 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; 
} 
+2

看来你创建你的按钮,未经界面生成器,那么为什么不从你的_ @属性中删除IBOutlet _?只是一个建议。 ;) – Kjuly

+0

oooooooh,这就是IBOutlet的意思! – squarefrog

+1

噢,IB(Interface Builder)插座〜;) – Kjuly

回答

2

而不是

mButton.selected = YES; 

使用

[mButton setHighlighted:YES]; 
+0

一旦我设置了突出显示状态,完美工作。 – squarefrog