ios UITableViewcell设置复选标记,但滚动时间复选标记是自动隐藏并尝试解决它崩溃

时间:2014-04-18 07:12:09

标签: ios objective-c uitableview

我有一个tableview单元格,这是从一个数组绑定并选择多个单元格并设置为复选标记但是当我滚动表格时,复选标记是自动隐藏 然后我搜索stackoverflow,我找到一个答案 链接是 UITableview accessory type disappear while scrolling

我试过这段代码,但应用程序崩溃这是我的代码,请帮帮我

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

    static NSString *simpleTableIdentifier = @"FirstTableViewCell";

   FirstTableViewCell  *cell = (FirstTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    if(tableView ==nurseTypeTable){
        cell.lbl1.text = [arrayNurseType objectAtIndex:indexPath.row];

        if(_nurseArray.count != nil){
      //  if (_nurseArray == nil || [_nurseArray count] == 0) {
        NSNumber *rowNsNum = [_nurseArray objectAtIndex:indexPath.row];
        if ( [arrayNurseType containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        }

        return cell;
    }
}
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellValue=nil;
    if (tableView==nurseTypeTable) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        //Here is for Check
        if(cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            //here i add  in array
            cellValue=[arrayNurseType objectAtIndex:indexPath.row];
            NSLog(@"%@",cellValue);

            [_nurseArray addObject:cellValue];
            NSLog(@"%@",_nurseArray);

        }
        //Here is for uncheck
        else {
            cellValue=[arrayNurseType objectAtIndex:indexPath.row];
            [_nurseArray removeObject:cellValue];
            cell.accessoryType = UITableViewCellAccessoryNone;
            NSLog(@"Remove Array  %@",_nurseArray);
        }
        // [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }

在函数 cellForRowAtIndexPath **

NSNumber *rowNsNum = [_nurseArray objectAtIndex:indexPath.row];** is creash on debug time 
arrayNurseType = [NSMutableArray arrayWithObjects:@" Nurse ",@"One",nil]

1 个答案:

答案 0 :(得分:1)

你应该使用

     if(tableView ==nurseTypeTable){
        cell.lbl1.text = [arrayNurseType objectAtIndex:indexPath.row];

        if(_nurseArray.count != nil){
      //  if (_nurseArray == nil || [_nurseArray count] == 0) {
        if ( [_nurseArray containsObject:indexPath]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        }

        return cell;
    }
}