2010-08-09 54 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