UITableView 中 UIButton 的选定状态

发布于 2024-09-13 08:13:20 字数 2930 浏览 6 评论 0原文

我在 UITableView 的每个单元格中都有 UIButton 。当我触摸它时,它的状态设置为选定。但是,当我滚动表视图以使按钮不可见时,状态将设置为正常。如何才能使 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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-09-20 08:13:20

将按钮状态与表视图分开保存在模型中的某个位置,并再次在 cellForRowAtIndexpath: 方法中设置按钮状态。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文