保存UITableviewcell accessoryView的状态

时间:2012-04-24 19:37:22

标签: iphone xcode ios5 uitableview uibutton

我的UITableViewCell中有一个checkboxView作为accessoryView。当我点击复选框时,它会被勾选,反之亦然。但当我向下滚动视图并向后滚动时,复选框消失了。 我想知道保存按钮状态的方法是什么?

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

编辑:

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

1 个答案:

答案 0 :(得分:1)

感谢发布代码。是的,您的问题是您正在cellForRowAtIndexPath方法中创建UIButton实例。这是不允许的(或者更确切地说,当您注意到时会发生奇怪的事情)。您应该创建一个TableViewCell子类并在那里添加按钮,或者为表格单元格创建一个XIB并加载它。

查看这篇文章以获得一些帮助:

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