如何在IOS中使用可重复使用的单元格

前端之家收集整理的这篇文章主要介绍了如何在IOS中使用可重复使用的单元格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好.我似乎不能很好地了解桌面视图的工作原理.有人可以向我解释细胞在桌面中是否重复使用,特别是滚动时?关于这一点的一个主要痛点是,当我在一个单元格中创建一个动作时,其他单元格会在滚动时受到影响.我尝试使用一个数组作为模型的后端,但是我仍然想到,当不考虑时,它会改变细胞.需要弄清楚的是,当阵列中的模型没有改变时,为什么会改变它们.

一个简单的例子:

表视图单元格与按钮“喜欢”.当我单击其中一个单元格中的按钮时,按钮文本更改为“不同”(到目前为止这么好).但是当我向下滚动时,即使我没有选择它们,其他单元格也显示“不同”.当我向上滚动时,我最初选择的单元格再次更改,较新的单元格也会更改.

我似乎没有想到这一点.如果你能给我一个工作示例的源代码,那将是真棒!谢谢!

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5.  
  6. likeState = [[NSMutableArray alloc]init];
  7.  
  8. int i =0;
  9. for (i=0; i<20; i++) {
  10. [likeState addObject:[NSNumber numberWithInt:0]];
  11. }
  12. }
  13. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  14. {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  15.  
  16. UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  17. [myButton setTitle:@"Like" forState:UIControlStateNormal];
  18. [myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
  19. myButton.frame = CGRectMake(14.0,10.0,125.0,25.0);
  20. myButton.tag =indexPath.row;
  21. [cell.contentView addSubview:myButton];
  22.  
  23. if (cell ==nil) {
  24.  
  25.  
  26. }
  27.  
  28. if ([[likeState objectAtIndex:indexPath.row]boolValue]==NO) {
  29. [myButton setTitle:@"Like" forState:UIControlStateNormal];
  30.  
  31. }
  32. else{
  33. [myButton setTitle:@"Unlike" forState:UIControlStateNormal];
  34.  
  35.  
  36. }
  37.  
  38.  
  39. return cell;
  40. }
  41. -(void)tapped:(UIButton *)sender{
  42.  
  43. [likeState replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithInt:1]];
  44.  
  45. [sender setTitle:@"Unlike" forState:UIControlStateNormal];
  46.  
  47. }

解决方法

我假设你是通过Storyboard来做的,因为你没有通过Interface Builder创建你的按钮,所以你需要检查一下被重新使用的单元格是否已经有按钮了.
根据您当前的逻辑,您正在创建一个新的按钮实例,一直在单元格重新出现.

我建议如下:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. static NSString *CellIdentifier = @"Cell";
  4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  5.  
  6. //following is required when using XIB but not needed when using Storyboard
  7. /*
  8. if (cell == nil) {
  9. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  10. }
  11. */
  12. //Reason:
  13. //[1] When using XIB,dequeueReusableCellWithIdentifier does NOT create a cell so (cell == nil) condition occurs
  14. //[2] When using Storyboard,dequeueReusableCellWithIdentifier DOES create a cell and so (cell == nil) condition never occurs
  15.  
  16. //check if cell is being reused by checking if the button already exists in it
  17. UIButton *myButton = (UIButton *)[cell.contentView viewWithTag:100];
  18.  
  19. if (myButton == nil) {
  20. myButton = [UIButton buttonWithType:UIButtonTypeCustom];
  21. [myButton setFrame:CGRectMake(14.0,25.0)];
  22. [myButton setTag:100]; //the tag is what helps in the first step
  23. [myButton setTitle:@"Like" forState:UIControlStateNormal];
  24. [myButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  25. [myButton addTarget:self action:@selector(tapped:andEvent:) forControlEvents:UIControlEventTouchUpInside];
  26. [cell.contentView addSubview:myButton];
  27.  
  28. NSLog(@"Button created");
  29. }
  30. else {
  31. NSLog(@"Button already created");
  32. }
  33.  
  34. if ([likeState[indexPath.row] boolValue]) {
  35. [myButton setTitle:@"Unlike" forState:UIControlStateNormal];
  36. }
  37. else {
  38. [myButton setTitle:@"Like" forState:UIControlStateNormal];
  39. }
  40.  
  41. return cell;
  42. }
  1. -(void)tapped:(UIButton *)sender andEvent:(UIEvent *)event
  2. {
  3. //get index
  4. NSSet *touches = [event allTouches];
  5. UITouch *touch = [touches anyObject];
  6. CGPoint currentTouchPosition = [touch locationInView:self.tableView];
  7. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
  8.  
  9. //toggle "like" status
  10. if ([likeState[indexPath.row] boolValue]) {
  11. [likeState replaceObjectAtIndex:indexPath.row withObject:@(0)];
  12. [sender setTitle:@"Like" forState:UIControlStateNormal];
  13. }
  14. else {
  15. [likeState replaceObjectAtIndex:indexPath.row withObject:@(1)];
  16. [sender setTitle:@"Unlike" forState:UIControlStateNormal];
  17. }
  18. }

猜你在找的iOS相关文章