views:

251

answers:

1

Hello,

I have UIButton in each cell of UITableView. When I touch it, its state is set to selected. But when I scroll table view so the button isn't visible, the state is set to normal. How can I do it that UIButton remain selected?

Thank you.

Edit: Here is the cellForRowAtIndexPath code:

- (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;

}

s

+1  A: 

Save button states somewhere in your model separately from table view and set buttons state in cellForRowAtIndexpath: method again.

Vladimir
Thank you for your reply. But can you please tell me, how to detect that state of UIButton has been changed?
Alex-alex
button state changes when user taps it, doesn't it? So you should do that in tap handler
Vladimir
No, I think when the state changed itself from selected to normal.
Alex-alex
Could you post the code for cellForRowAtIndexpath? When you reuse table cell your button state may get reset for some reason, button can't change its state itself
Vladimir
OK, the code is in main post.
Alex-alex