UITableviewcell中的复选框随着表格滚动而消失

时间:2013-01-14 20:38:31

标签: iphone objective-c xcode uitableview

我正在尝试在每个uitableviewcell中创建一个可以在所选和未选中之间切换的复选框。为了确保在加载新单元格时单个选择不再循环,我创建了一个可变的字符串数组,以表示“取消选中”或“检查”(即,当按钮图像应该未选中或选中时)。按下检查按钮时,字符串将被“检查”或“取消选中”替换。由于某种原因我无法看到,当桌面视图滚动并且带有所选检查按钮的单元格离开屏幕时,数组会丢失对其进行的更改,因此检查字符串将丢失。

任何想法有什么不对?

提前感谢您的帮助。

在“checkButtonClicked”中检查第一个单元格中的按钮后面的selectedCheckArray的NSLog: {Check,Uncheck,Uncheck,Uncheck,Uncheck,Uncheck,Uncheck}

向下滚动后,selectedCheckArray的NSLog,以便屏幕上不再显示第一个单元格: {取消选中,取消选中,取消选中,取消选中,取消选中,取消选中,取消选中}

以下是代码:

·H

    @property (strong, nonatomic) NSMutableArray *selectedCheckArray;

的.m

    @synthesize selectedCheckArray;

...

    - (void)viewWillAppear:(BOOL)animated
    {
        // cellDataArray loaded here

        selectedCheckArray = [[NSMutableArray alloc] init];

        for (int i = 0; i<[cellDataArray count]; i++) {
            [selectedCheckArray addObject:@"Uncheck"];
        } 

        [super viewWillAppear:animated];
    } 


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [cellDataArray count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *CellIdentifier = @"choiceCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        UIButton *checkButton = (UIButton *)[cell viewWithTag:1];

        if ([[selectedCheckArray objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])         {
            [checkButton setImage:[UIImage imageNamed:@"unchecked"]   
               forState:UIControlStateNormal];
        } else {
            [checkButton setImage:[UIImage imageNamed:@"checked"] 
               forState:UIControlStateNormal];
        }
        [checkButton addTarget:self action:@selector(checkButtonClicked:)  
           forControlEvents:UIControlEventTouchUpInside];

        return cell;
    }

...

    - (void)checkButtonClicked:(id)sender
    {
        // indexPath of cell of clicked button
        CGPoint touchPoint = [sender convertPoint:CGPointZero toView:choiceTable];
        NSIndexPath *indexPath = [choiceTable indexPathForRowAtPoint:touchPoint];

        // Not using tag as sender will keep reference of clicked button
        UIButton *button = (UIButton *)sender;

        //Checking the condition button is checked or unchecked.
        //accordingly replace the array object and change the button image
        if([[selectedCheckArray objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
        {
            [button setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
            [selectedCheckArray replaceObjectAtIndex:indexPath.row withObject:@"Check"];
        } else {
            [button setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
            [selectedCheckArray replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个,

<。>在.h文件中,

NSMutableArray *arrayCellChecked;

@property(nonatomic, retain) NSIndexPath *lastIndexPath;
<。>文件中的

@synthesize lastIndexPath;
int oldRow, newRow;

在DidLoad方法中,

arrayCellChecked = [[NSMutableArray alloc] init];

表行方法,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        //Your Code
        ----------

        newRow = [indexPath row];
        oldRow = [[self lastIndexPath] row];


        cell.accessibilityViewIsModal = (newRow == oldRow && lastIndexPath != nil) ? NO : YES;

        if(cell.accessibilityViewIsModal == YES)
            [checkButton setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
        else
            [checkButton setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];

        //Your Method for Button Click
        -----------

        return cell;
}

表格单元选择方法,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Your Cell Code
    -------

    if (cell.accessibilityViewIsModal == NO) {

        cell.imgCheck.image = [UIImage imageNamed:@"checked"];
        cell.accessibilityViewIsModal = YES;

        NSString *cellValue = yourArray.object;

        [arrayCellChecked addObject:cellValue];

        -------
    }

    else{

        cell.imgCheck.image = [UIImage imageNamed:@"unchecked"];
        cell.accessibilityViewIsModal = NO;

        NSString *cellValue = yourArray.object;

        [arrayCellChecked removeObject:cellValue];

        --------
    }
 }

它将为'didSelectRowAtIndex'完成。不需要按钮点击方法。

我已经用这种方式解决了它。

希望它对你有用。 感谢。