2010-08-09 2 views
1

У меня есть UIButton в каждой ячейке UITableView. Когда я касаюсь его, его состояние будет выбрано. Но когда я просматриваю табличный вид, поэтому кнопка не видна, состояние устанавливается в нормальное состояние. Как я могу это сделать, что UIButton остаются выбранными?Выбранное состояние UIButton в UITableView

спасибо.

Edit: Вот 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 было изменено? –

+0

Состояние кнопки меняется, когда пользователь нажимает кнопку, не так ли? Поэтому вы должны сделать это в обработчике крана – Vladimir

+0

Нет, я думаю, когда состояние изменилось из выбранного в нормальное. –