IOS TableView :Table View Custom Cell While Scrolling It pre-populates the Value in another cell

标签 ios objective-c uitableview

创建单元格:

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

    AddEventsCell *cell= 
      [tableView dequeueReusableCellWithIdentifier:@"addevents"];
    NSLog(@"cell %@",cell);
    if(cell==nil){
        [tableView registerNib:[UINib nibWithNibName:@"addevents" 
                                              bundle:nil] 
        forCellReuseIdentifier:@"addevents"];
        cell=[tableView dequeueReusableCellWithIdentifier:@"addevents"];


    }
     cell.tag=indexPath.row;
    return cell;
}

显示单元格:

-(void) tableView:(UITableView *)tableView 
  willDisplayCell:(AddEventsCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"index path %d == %d",indexPath.row,cell.tag);
    if(cell.tag==0){

        cell.lableFiledValue.text=@"Event Name";
        cell.textFieldValue.placeholder=@"0";
        [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==1){
        cell.lableFiledValue.text=@"Category";
        cell.textFieldValue.placeholder=@"1";
         [self.textfieldArray addObject:cell.textFieldValue];
    }else if(cell.tag==2){
        cell.lableFiledValue.text=@"Upload Image";
        cell.textFieldValue.placeholder=@"2";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==3){
        cell.lableFiledValue.text=@"Time";
        cell.textFieldValue.placeholder=@"3";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==4){
        cell.lableFiledValue.text=@"Location";
        cell.textFieldValue.placeholder=@"4";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==5){
        cell.lableFiledValue.text=@"Description";
        cell.textFieldValue.placeholder=@"5";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==6){
        cell.lableFiledValue.text=@"Factilitator";
        cell.textFieldValue.placeholder=@"6";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==7){
        cell.lableFiledValue.text=@"Price";
        cell.textFieldValue.placeholder=@"7";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==8){
        cell.lableFiledValue.text=@"Notes";
        cell.textFieldValue.placeholder=@"8";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==9){
        cell.lableFiledValue.text=@"Duration";
        cell.textFieldValue.placeholder=@"9";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==10){
        cell.lableFiledValue.text=@"Program";
        cell.textFieldValue.placeholder=@"10";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==11){
        cell.lableFiledValue.text=@"pre-requiment";
        cell.textFieldValue.placeholder=@"11";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==12){
        cell.lableFiledValue.text=@"Target";
        cell.textFieldValue.placeholder=@"12";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==13){
        cell.lableFiledValue.text=@"Contact";
        cell.textFieldValue.placeholder=@"13";
          [self.textfieldArray addObject:cell.textFieldValue];

    }else if(cell.tag==14){
        cell.lableFiledValue.text=@"Website";
        cell.textFieldValue.placeholder=@"14";
          [self.textfieldArray addObject:cell.textFieldValue];
    }


}

所有单元格对象都添加到self.textfieldArray

如果我在第一个框中输入值,意味着它将在第 7 个框中显示相同的值。

停止预填充数据。

最佳答案

AddEventsCell中实现prepareForReuse:

-(void)prepareForReuse {
    self.lableFiledValue.text = nil;
    self.textFieldValue.placeholder = nil;
}

另一件事 - willDisplayCell 每次 tableView 将要显示单元格时调用 - 这意味着当用户来回滚动 tableView 时同一单元格多次。您可能想找到另一种方法来将项目添加到 textfieldArray。您可以使用 NSMutableArraysetObject:atIndexedSubscript:,或者在添加之前检查给定对象是否已存在于数组中。

关于IOS TableView :Table View Custom Cell While Scrolling It pre-populates the Value in another cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170414/

相关文章:

ios - didAddNode 与 SceneKit 碰撞检测

ios - 需要随着底部列表滚动移动顶部红色选择器

iphone - 从字符串中检索图像 url

ios - 如何在 UITextfield 中禁用复制和定义 UIMenuController 的 UIMenuItems

ios - XCode 颜色差异

iphone - 在 iOS 中检查文件创建日期时出现问题

ios - iPhone Contact 应用程序如何检测用户是否有链接到 WhatsApp 的号码?

ios - UITableViewCells 未出现在第二个选项卡中

ios - 快速将 UILabel 的内容包装在一个行单元格中

iphone - 点击 uitableviewcell iOS 后加载 uiviewcontroller