简体   繁体   English

保存UITableviewcell annexView的状态

[英]saving the state of UITableviewcell accessoryView

I have a checkbox as an accessoryView in my UITableViewCell. 我的UITableViewCell中有一个复选框作为附件视图。 When I click on the checkbox, it gets checkmarked and vise versa. 当我单击复选框时,它会被选中,反之亦然。 But When i scroll down out of view and scroll back, the checkbox is gone. 但是,当我向下滚动到视图之外并向后滚动时,该复选框消失了。 I am wondering what are some ways to save the state of my button? 我想知道有哪些方法可以保存按钮的状态?

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;

    if([tableView isEqual:self.myTableView]){
        static NSString *TableViewIdentifier = @"MyCells";
        cell = [tableView dequeueReusableCellWithIdentifier:TableViewIdentifier];
        if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:TableViewIdentifier];
        } 
        //configure the cell
        checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
        [checkbox setFrame:checkboxRect];  
        [checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
        [checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];

        /*if (checkbox.state == UIControlStateNormal) {
            [checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateHighlighted];
        }else if(checkbox.state == UIControlStateSelected){
            [checkbox setImage:[UIImage imageNamed:@"selected@2x.png"]forState:UIControlStateHighlighted];
        }*/

        [checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = checkbox;


        NSString *group;
        NSUInteger row = [indexPath row];

        switch (indexPath.section) {
            case 0:
                group = [suggestedPeople objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 1:
                group = [aArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 2:
                group = [bArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 3:
                group = [cArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 4:
                group = [dArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 5:
                group = [eArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
        }
    }
    return cell;
}

EDIT: 编辑:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
    checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [checkbox setFrame:checkboxRect];  
    [checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];

}
return self;}

Thanks for posting the code. 感谢您发布代码。 Right, your problem is that you are creating UIButton instances in the cellForRowAtIndexPath method. 正确,您的问题是您正在cellForRowAtIndexPath方法中创建UIButton实例。 This isn't allowed (or rather, odd things will happen as you've noticed). 这是不允许的(或者相反,您会注意到奇怪的事情会发生)。 You should create a TableViewCell subclass and add the button there, or create a XIB for the table cell and load that. 您应该创建一个TableViewCell子类并在其中添加按钮,或者为表单元格创建一个XIB并加载它。

Check out this post for some help with this: 请查看此帖子以获取有关此方面的帮助:

http://forums.macrumors.com/showthread.php?t=545061 http://forums.macrumors.com/showthread.php?t=545061

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM