iphone表视图复选标记附件问题 [英] iphone table view check mark accessory problem

查看:98
本文介绍了iphone表视图复选标记附件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含50行的表格。我想选择带有复选标记附件的特定行,但是当我选择一些行并向下滚动表格时,我会看到预先选中的行。我知道表格单元格被重用但我想发出这个问题该怎么办呢?

I have a table with 50 rows. I want to select particular rows with checkmark accessory but when i select some rows and scroll down the table then i see pre checked rows. I know that table cell are reused but i want to emit this problem what can i do about this?

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

  TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }
    cell.textLabel.text=[NSString stringWithFormat:@"%i",[indexPath row]];
    return cell;
}

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


推荐答案

您必须设置单元格。 accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

You have to set cell.accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

事实是,因为你有一个重用标识符,当一个新的请求单元格,复制具有标识符的单元格。由于您在最新单元格中设置了复选标记,因此复制了检查掩码。

The fact is that as you have a reuse identifier, when a new cell is requested, a cell with the identifier is copied. As you have set the checkmark in the latest cell, the checkmask is copied.

您必须存储每个单元格的状态(即在数组中)以重新创建单元格具有正确的价值。

You have to store the state of each cell (ie in an array) to recreate the cell with the right value.

类似的东西:

BOOL values[50]; //Not the best way, but easy for the example...


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
  TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  cell.textLabel.text=[NSString stringWithFormat:@"%i",[indexPath row]];
  if (value[indexPath.row]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  } else {
    cell.accessoryType = UITableViewCellAccessoryNone;
  }
  return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

  values[indexPath.row] = !value[indexPath.row];

  if (value[indexPath.row]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  } else {
    cell.accessoryType = UITableViewCellAccessoryNone;
  }
}

这篇关于iphone表视图复选标记附件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆