ios - tableView 刷新时丢失自定义数据

标签 ios uitableview custom-cell pull-to-refresh

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

  ItemsCustomCell *cell; 
  static NSString *CellIdentifier = @"Cell"; 
  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  if (cell == nil)   {
     cell = [[ItemsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemsCustomCell" owner:self options:nil];
  cell = [nib objectAtIndex:0];
  cell.itemName.text=titles[indexPath.row];

  [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];    
  [cell.addButton addTarget:self action:@selector(Checktag:event:)forControlEvents:UIControlEventTouchUpInside];
  cell.cellBackgroudImage.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"list_bg.png"] ];


  NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.itemsTableView indexPathsForVisibleRows]];

  NSIndexPath *indexPathVisibleRowsLast= [anArrayOfIndexPath lastObject];

  [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
  int firstVisible=(indexPathVisibleRowsLast.row)-8;   
  CGPoint offset = itemsTableView.contentOffset;
  CGRect bounds = itemsTableView.bounds;
  CGSize size = itemsTableView.contentSize;
  UIEdgeInsets inset = itemsTableView.contentInset;
  float y = offset.y + bounds.size.height - inset.bottom;
  float h = size.height;
  float reload_distance = 10;       

  }        
}  
return cell;

}

此代码用于在单击时更改单元格 .button 的图像
- (void)Checktag:(id)sender event:(id)event 
{   
        UIButton *btnClicked=(UIButton *)sender;
        NSLog(@"%i",btnClicked.tag);
        [ btnClicked setBackgroundImage:[UIImage imageNamed:@"tick_btn.png"]
                                   forState:UIControlStateNormal];
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:itemsTableView];
        NSIndexPath *indexPath = [itemsTableView indexPathForRowAtPoint: currentTouchPosition];

        NSString *title=titles[indexPath.row];
        NSString *id1=idAll[indexPath.row];
        [mySelectedItems addObject:title];
        [idSelectedItems addObject:id1];
        NSLog(@"value of indePath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);    
}

当表刷新时,所有图像的更改都设置为默认值

最佳答案

查看代码的最后几行

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

    ItemsCustomCell *cell;
    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)   {
        cell = [[ItemsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemsCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


        [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
        [cell.addButton addTarget:self action:@selector(Checktag:event:)forControlEvents:UIControlEventTouchUpInside];
        cell.cellBackgroudImage.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"list_bg.png"] ];


        NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.itemsTableView indexPathsForVisibleRows]];

        NSIndexPath *indexPathVisibleRowsLast= [anArrayOfIndexPath lastObject];

        [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
        int firstVisible=(indexPathVisibleRowsLast.row)-8;
        CGPoint offset = itemsTableView.contentOffset;
        CGRect bounds = itemsTableView.bounds;
        CGSize size = itemsTableView.contentSize;
        UIEdgeInsets inset = itemsTableView.contentInset;
        float y = offset.y + bounds.size.height - inset.bottom;
        float h = size.height;
        float reload_distance = 10;       

    }
    cell.itemName.text=titles[indexPath.row];

    if ([idSelectedItems containsObject:indexPath])
    {
        [ cell.addButton setBackgroundImage:[UIImage imageNamed:@"tick_btn.png"]
                               forState:UIControlStateNormal];
    }

}  

关于ios - tableView 刷新时丢失自定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23292137/

相关文章:

ios - 我从哪里得到 "valid Oauth 2.0 token for the service account of the Firebase project"

ios - 如果 AppleWebKit 出现在用户代理中,是否意味着它肯定是苹果设备?

ios - iOS 5 中 UIScrollView layoutSubviews 行为发生变化?

ios - 为什么我的 Swift 中的正则表达式无法编译?我该如何给它一个可变字符串呢?

ios - 当 ViewController 取消初始化时,UITableView/UIScrollView 委托(delegate)未设置为 nil

iphone - 处理 UITableViewCell 中的按钮

ios - 修改单元格 subview 的框架

ios - 无法将 UITableViewCell 设置为单元格

ios - 自动 UITableViewCell 高度的奇怪结果

ios - 如何在一个 tableView 上提供 2 个以上的自定义单元格进行聊天