如何添加一个复选框到UITableView在左边,同时使用UITableViewCell缓存? [英] How to add a checkbox to a UITableView on the left, while using UITableViewCell caching?

查看:246
本文介绍了如何添加一个复选框到UITableView在左边,同时使用UITableViewCell缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个麻烦,试图实现一个复选框作为todo列表的一部分。
我有一个UITableView,每个UITableViewCell应该有自己的复选框,在左侧。



我会感激,如果有人可以告诉我如何完成这个。我已经详细地查看了本网站上的问题,但仍然无法弄清楚如何实现这一点。



滚动tableview时出现问题。如果我有20个todo项目,例如,当我滚动时,要选择,取消选择,并且取消选择的复选框,以看似随机的方式选择。



当系统重用单元格时,缓存和发生了什么?我只抓住一个空单元格,只引用其中的空子视图?



如果tablecell#1有一个标签为10的视图,tablecell#2将能够有一个标签为10的视图?还是标签不能在整个tableView中重复? (我不确定这是问题)



谢谢!

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

switch(selectedSegment){

case Todos:

static NSString * ToDoCellIdentifier = @TodoTableViewCell;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifierToDoCellIdentifier];

UITextView * taskTextView = nil;
UIButton * checkBox = nil;
UIImageView * highPriorityIcon = nil;

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


checkBox = [[UIButton alloc] initWithFrame:CGRectMake(1,1,25,25)];
checkBox.tag = indexPath.row;
UIImage * normalImage = [UIImage imageNamed:@checkbox_unchecked@2x.png];

UIImage * selectedImage = [UIImage imageNamed:@checkbox_checked@2x.png];
[checkBox setImage:normalImage forState:UIControlStateNormal];
[checkBox setImage:selectedImage forState:UIControlStateSelected];

[checkBox addTarget:self action:@selector(historyButtonPressed :) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:checkBox];
[checkBox release];

//cell.contentView.backgroundColor = [UIColorlightGrayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(@创建新单元格);

}

if(!checkBox){

checkBox =(UIButton *)[cell viewWithTag:indexPath.row];
}


}


if([[self.tasksCheckedStates objectAtIndex:indexPath.row] boolValue]){



checkBox.selected = YES;

}
else {


checkBox.selected = NO;
}



return cell;
break;
}


解决方案




  • 将已选中/未选中的状态存储在后台数据模型中,而不仅仅是单元格的属性。单元格被重用,因此您不能使用单元格的属性进行持久化。

  • 在您的cellForRowAtIndexPath方法中,请确保取消选中未选中项目的指示器(但是您正在实现此选项),以及检查选中项目的指示器。再次,单元被重用,因此被检查的单元可以被出列未被检查的项目。



EDIT



因为我在写我的答案!我认为你的问题是你分配给复选框的标签。你不应该使用索引路径行为此,因为你将没有引用这一点,当你向下滚动到,例如,行20.给复选框所有单元格的相同的标签,你应该确定。您可以使用标记来区分超级视图中的视图,因此在所有单元格中具有相同标记的复选框没有问题。



您的另一个问题的另一点:当单元格从表中出列时,您将返回原始对象,其所有视图和数据用于上一个时间。因此,如果除了出队单元格之外没有什么,创建它,如果丢失,然后返回它,你的第一屏数据将只是不断重复。除了标记问题,您已经关注的正常操作过程为




  • 出列单元

  • 如果没有返回单元格,请创建新单元格并执行基本的不变更配置。

  • 为特定行设置单元格


    • I am having quite a bit of trouble trying to implement a checkbox as part of a todo list. I have a UITableView, and each UITableViewCell should have its own checkbox, on the lefthand side.

      I would appreciate it if someone could show me how to accomplish this. I have looked extensively at the questions on this site and still can't figure out how to implement this.

      The issues arise when scrolling the tableview. If I have 20 todo items, for example, when I scroll, the checkboxes that are to be selected, get deselected, and the ones that are to be deselected, get selected, in a seemingly random fashion. It must be something with the scrolling.

      What exactly is being cached and happening when the system reuses a cell? Am I grabbing an empty cell with just references to empty subviews inside it?

      If tablecell #1 has a view with a tag of 10, will tablecell #2 be able to have a view with a tag of 10? Or are tags not to be repeated throughout the entire tableView? (I'm not even sure that this is the problem)

      Thank you!

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
      switch (selectedSegment) {
      
      case Todos:{
      
                  static NSString *ToDoCellIdentifier = @"TodoTableViewCell";
                  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifierToDoCellIdentifier];
      
                  UITextView *taskTextView = nil;
                  UIButton *checkBox = nil;
                  UIImageView *highPriorityIcon = nil;
      
                  if (cell == nil) {
                      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:signoutTableViewCellIdentifier] autorelease];
      
      
                      checkBox = [[UIButton alloc]initWithFrame:CGRectMake(1, 1, 25, 25)];
                      checkBox.tag = indexPath.row;
                      UIImage *normalImage = [UIImage imageNamed: @"checkbox_unchecked@2x.png"];
      
                      UIImage *selectedImage = [UIImage imageNamed: @"checkbox_checked@2x.png"];
                      [checkBox setImage:normalImage forState:UIControlStateNormal];
                      [checkBox setImage:selectedImage forState:UIControlStateSelected];
      
                      [checkBox addTarget:self action:@selector(historyButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
                      [cell.contentView addSubview:checkBox];
                      [checkBox release];
      
                      //cell.contentView.backgroundColor = [UIColor lightGrayColor];
                      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                      NSLog(@"creating a new cell");
      
                  }
      
                  if (!checkBox) {
      
                      checkBox = (UIButton*)[cell viewWithTag:indexPath.row];
                  }
      
      
                  }
      
      
                  if ([[self.tasksCheckedStates objectAtIndex:indexPath.row]boolValue]) {
      
      
      
                      checkBox.selected = YES;
      
                  }
                  else {
      
      
                      checkBox.selected = NO;
                  }
      
      
      
                  return cell;
                  break;
              }
      

      解决方案

      You need to do the following:

      • store the checked / unchecked status in your background data model, not just as a property of your cells. The cell is reused so you can't use properties of the cell for persistence.
      • in your cellForRowAtIndexPath method, make sure that you uncheck your indicator (however you are implementing this) for unchecked items, as well as checking the indicator for checked items. Again, the cells are reused so a checked cell could be dequeued for an unchecked item.

      EDIT

      You posted your code as I was writing my answer! I think your problem is the tag you are assigning to the checkbox. You shouldn't be using the index path row for this, as you will have no reference to this by the time you have scrolled down to, say, row 20. Give the checkbox the same tag for all cells and you should be ok. You use the tags to distinguish between views within a superview, so there is no problem with the checkbox having the same tag in all cells.

      A further point on your other question: when a cell is dequeued from the table, you get the original object back, with all its views and data as used for the previous time. So if you did nothing except dequeue a cell, create it if missing, then returned it, your first screenful of data would just keep repeating. The normal course of operations, which you are already following except for the tagging issue, is

      • dequeue cell
      • if no cell is returned, create a new cell and do basic, non- changing configuration
      • set up the cell for the specific row

      这篇关于如何添加一个复选框到UITableView在左边,同时使用UITableViewCell缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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