2010-08-09 40 views
1

我在UITableView的每個單元中都有UIButton。當我觸摸它時,其狀態設置爲選中狀態。但是,當我滾動表視圖,使按鈕不可見時,狀態設置爲正常。我怎麼能這樣做,UIButton仍然選擇?UITableView中的UIButton的選定狀態

謝謝。

編輯:這裏是cellForRowAtIndexPath代碼:某處

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

static NSString *CellIdentifier = @"Cell"; 

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

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

if (indexPath.row < [messagesArray count]) { 

    Zprava *msgObj = [messagesArray objectAtIndex:indexPath.row]; 

    int width = 0; 

    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) { 
     width = 320; 
    } else { 
     width = 480; 
    } 

    CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX); 
    CGSize requiredSize = [msgObj.mmessage sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap]; 

    UIButton *background = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 300, requiredSize.height + 20)]; 
    [background setBackgroundImage:[[UIImage imageNamed:@"balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateNormal]; 
    [background setBackgroundImage:[[UIImage imageNamed:@"selected-balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateSelected]; 
    [background addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
    background.tag = msgObj.msgID; 
    [[cell contentView] addSubview:background]; 
    [background release]; 

    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 285, 15)]; 
    [dateLabel setFont:[UIFont systemFontOfSize:12]]; 
    [dateLabel setLineBreakMode:UILineBreakModeWordWrap]; 
    [dateLabel setTextColor:[UIColor lightGrayColor]]; 
    [dateLabel setNumberOfLines:0]; 
    [dateLabel setTextAlignment:UITextAlignmentRight]; 
    [dateLabel setText:msgObj.mdate]; 
    [dateLabel setBackgroundColor:[UIColor clearColor]];  
    [dateLabel setOpaque:NO]; 
    [[cell contentView] addSubview:dateLabel]; 
    [dateLabel release]; 

    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 275, requiredSize.height + 5)]; 
    [messageLabel setFont:[UIFont systemFontOfSize:17]]; 
    [messageLabel setLineBreakMode:UILineBreakModeWordWrap]; 
    [messageLabel setTextColor:[UIColor blackColor]]; 
    [messageLabel setNumberOfLines:0]; 
    [messageLabel setText:msgObj.mmessage]; 
    [messageLabel setBackgroundColor:[UIColor clearColor]]; 
    [messageLabel setOpaque:NO]; 
    [[cell contentView] addSubview:messageLabel]; 
    [messageLabel release]; 

} 

return cell; 

} 

回答

2

保存按鈕狀態模型中的獨立表視圖,並重新設置按鈕狀態cellForRowAtIndexpath:方法。

+0

謝謝您的回覆。但你能告訴我,如何檢測UIButton的狀態已被改變? – 2010-08-09 14:25:18

+0

當用戶點擊按鈕時,按鈕狀態會改變,不是嗎?所以你應該這樣做,在水龍頭處理器 – Vladimir 2010-08-09 14:31:30

+0

不,我認爲當狀態從選中改爲正常。 – 2010-08-09 14:34:30

相關問題