uitableview复选框重置问题

use*_*878 0 iphone ios4

我有20行的uitableview,显示了许多食品.

每个食品都有一个复选框.

我的问题是:如果我检查复选框的第一行,然后滚动tableview,复选标记.

我怎么解决这个问题?请帮帮我.

代码更新:

- (IBAction)buttonAction:(id)sender
{
  if ([sender isKindOfClass:[UIButton class]])
  {
    UIButton *checkboxButton = (UIButton*)sender;

    checkboxButton.selected = !checkboxButton.selected;

    NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell*)[[checkboxButton superview] superview]];

    BOOL selected = [[selectedArray objectAtIndex:[indexPath row]] boolValue];

    [selectedArray replaceObjectAtIndex:[indexPath row] withObject:[NSNumber numberWithBool:!selected]];

      if (!self.checkedIndexPaths)
          checkedIndexPaths = [[NSMutableSet alloc] init];

    if(selected == NO)
    {
          NSLog(@"cvbcvbNO BOOL value");    // ...

        //  If we are checking this cell, we do
        [self.checkedIndexPaths addObject:indexPath];
    }
    else
    {
      NSLog(@"cvbvbYES BOOL VALURE");

        //  If we are checking this cell, we do
        [self.checkedIndexPaths removeObject:indexPath];
    }



  }
}

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

  static NSString *CellIdentifier = @"Celhgl";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  profileName = [appDelegate.sentItemsList objectAtIndex:indexPath.row];

  if (cell == nil)
  {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        cb = [[UIButton alloc] initWithFrame:CGRectMake(5,10, unselectedImage.size.width, unselectedImage.size.height)];
        [cb setImage:unselectedImage forState:UIControlStateNormal];
        [cb setImage:selectedImage forState:UIControlStateSelected];
        [cb addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
        [cell.contentView addSubview:cb];

        for (NSIndexPath *path in self.checkedIndexPaths)
        {
            NSLog(@"%d",path.row);

            NSLog(@"%d",indexPath.row);

           if (path.row == indexPath.row)
          {
            NSLog(@"dfd %d",indexPath.row);
          }
        }

   }

    if ( tableView == myTableView )
    {
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 150, 35)];
        titleLabel.font = [UIFont boldSystemFontOfSize:13];
        titleLabel.textColor = [UIColor blackColor];   
        [cell.contentView addSubview:titleLabel];
        NSString *subjectData = [profileName.sent_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [titleLabel setText:[NSString stringWithFormat: @"%@ ", subjectData]];
    }

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*uch 5

将检查的项目保存在数据源中.

我通常将NSIndexPaths所选对象保存在一个NSMutableSet.
tableView:cellForRowAtIndexPath:我检查索引路径是否是具有所选索引路径的集合的一部分.

@interface RootViewController : UITableViewController {
    NSMutableSet *set;
}

// implementation:

- (void)viewDidLoad {
    [super viewDidLoad];
    set = [[NSMutableSet alloc] init];
}

- (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 = [NSString stringWithFormat:@"Cell %d", indexPath.row];
    if ([set containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([set containsObject:indexPath]) {
        [set removeObject:indexPath];
    }
    else {
        [set addObject:indexPath];
    }
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Run Code Online (Sandbox Code Playgroud)


Mas*_*son 5

发生的事情是UITableView正在回收UITableViewCells以节省内存.这意味着当您向下滚动列表时,UITableView会将单元格从表格的顶部取出并重新使用它们以显示后面的项目,因此当您向上滚动它们时它们会丢失状态.

您可以通过保留已NSMutableSet检查的indexPath 来纠正此问题.当用户检查项目时,您将其添加indexPath到此集合.然后在您的工作中,cellForRowAtIndexPath您可以确保检查项目是否在您的已检查项目集中.

UPDATE

以下是这可能如何工作的一个粗略示例:

# MyTableView.h

@interface MyTableView: UITableView
<UITableViewDataSource, UITableViewDelegate>
{
  NSMutableSet *checkedIndexPaths;
}

@property (nonatomic, retain) NSMutableSet *checkedIndexPaths;

@end
Run Code Online (Sandbox Code Playgroud)

然后

# MyTableView.m
#import "MyTableView.h"

@implementation MyTableView

@synthesize checkedIndexPaths;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //  Normal layout stuff goes here...
  //  ***Add code to make sure the checkbox in this cell is unticked.***

  for (NSIndexPath *path in self.checkedIndexPaths)
  {
    if (path.section == indexPath.section && path.row == indexPath.row)
    {
      //  ***We found a matching index path in our set of checked index paths, so we need to show this to the user by putting a tick in the check box, for instance***
    }
  }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  //  Normal stuff to handle visual checking/unchecking of row here

  //  Lazy-load the mutable set
  if (!self.checkedIndexPaths)
    checkedIndexPaths = [[NSMutableSet alloc] init];

  //  If we are checking this cell, we do
  [self.checkedIndexPaths addObject:indexPath];

  //  If we are unchecking, just enumerate over the items in checkedIndexPaths and remove the one where the row and section match.
}

@end
Run Code Online (Sandbox Code Playgroud)

这只是骨架代码并没有经过测试,但希望它能给你一个jist.