【问题标题】:Set checkmark in UITableView [duplicate]在 UITableView 中设置复选标记 [重复]
【发布时间】:2013-06-29 10:39:59
【问题描述】:

我正在尝试在 UITableView 中选择的行中设置复选标记(放置在弹出视图中)。其实我有这样的东西:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Check if current row is selected
    Boolean isNowChecked = NO;
    if([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) 
    {
        isNowChecked = YES;
    }

    if(isNowChecked) 
    {
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    }
    else 
    {
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    }
    return indexPath;
}

结果我可以选择行,例如我触摸第一个项目然后我看到复选标记。当我向下滚动表格视图时,我看到我还检查了其他项目(编号 17、32)。当我取消选中我的第一个项目时,每个检查也会消失。

您对造成这种情况的原因有什么建议,我该如何避免?

【问题讨论】:

  • 你的tableView:cellForRowAtIndexPath: 看起来如何(即你如何实例化你的单元格)?
  • 我认为您没有使用 UITableViewCell 的适当可重用性。所以添加你的 cellForRowAtIndexPath 方法的代码
  • 是的,你是对的。我在 cellForRowAtIndexPath 中有错误的实现。

标签: ios objective-c uitableview ios6 xcode4


【解决方案1】:

// 试试这个 //在.h中

NSMutableArray *arr_fortable;

//在.m中

- (void)viewDidLoad
{
    [super viewDidLoad];

    arr_fortable = [[NSMutableArray alloc] init];
    for (int i =0; i<5; i++)
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:[NSString stringWithFormat:@"%d",i+1] forKey:@"data"];
        [dict setObject:@"0" forKey:@"checkmark"];
        [arr_fortable addObject:dict];
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arr_fortable count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"data"];
    if ([[[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"checkmark"] integerValue] == 1)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"checkmark"] integerValue] == 1)
    {
        [[arr_fortable objectAtIndex:indexPath.row] setObject:@"0" forKey:@"checkmark"];
    }
    else
    {
        [[arr_fortable objectAtIndex:indexPath.row] setObject:@"1" forKey:@"checkmark"];
    }
    [self.tableView reloadData];
}

【讨论】:

  • 感谢您的提示。我在 cellForRowAtIndexPath 中实现了错误的逻辑。现在一切正常。
  • @Lokesh Chowdary - 如何将它用于具有多个部分的 tableview?
【解决方案2】:

看到这个..

tableView:didSelectRowAtIndexPath: 并更新在 tableView:cellForRowAtIndexPath: 中检查了哪些数据,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // do usual stuff here including getting the cell

    // determine the data from the IndexPath.row

    if (data == self.checkedData)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the selected data from the IndexPath.row

    if (data != self.checkedData) {
       self.checkedData = data;
    }

    [tableView reloadData];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    相关资源
    最近更新 更多