2011-11-15 111 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