【问题标题】:Scrolling of tableview removes checkmarrk from uitableview cell滚动表视图从 uitableview 单元格中删除复选标记
【发布时间】:2013-02-10 11:37:10
【问题描述】:

我正在使用 tableview 并且正在对 uitableview 进行多次检查。一切都很完美,我得到了正确的值,但是当我滚动表格视图时,它丢失了复选标记图像(使用默认复选标记没有自定义图像)但选定的值保留在数组中......

滚动表格视图会移除复选标记图像。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app= (AppDelegate *)[[UIApplication sharedApplication]delegate];

    if([_tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){

    NSLog(@"yes");

    [placesvisitedarray removeObject:[app.nameArray objectAtIndex:indexPath.row]];


    [_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

    }
    else
    {
        NSLog(@"no");

        [_tableView cellForRowAtIndexPath:indexPath].accessoryType =  UITableViewCellAccessoryCheckmark;

        [placesvisitedarray addObject:[app.nameArray objectAtIndex:indexPath.row]];

    }
   // [_tableView reloadData];

}

【问题讨论】:

    标签: ios objective-c xcode4.5


    【解决方案1】:

    复选标记被删除,因为当您滚动时,正在调用 tableview cellForRowAtIndexPath: 并重新创建单元格。

    您可以编写一个方法来检查您的数组中是否存在某个值:

    - (BOOL)stringExistsInPlacesVisited:(NSString *)stringToMatch {
        for (NSString string in placesvisitedarray) {
            if ([string isEqualTo:stringToMatch])
                return YES;
        }
        return NO;
    }
    

    然后在cellForRowAtIndexPath: 你必须检查placesvisitedarray 并插入/删除复选标记。

    if ([stringExistsInPlacesVisited:[app.nameArray objectAtIndex:indexPath.row])
        cell.accessoryType =  UITableViewCellAccessoryCheckmark;
    else
       cell.accessoryType =  UITableViewCellAccessoryNone;
    

    代码未经测试,因此可能无法正常工作,但至少它可以让您了解如何继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多