IOS表视图:滚动时的表视图自定义单元格预先填充另一个单元格中的值

时间:2014-03-04 11:14:39

标签: 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

如果我在第1个框中输入值,则表示它将在第7个框中显示相同的值。

停止预先填充数据。

1 个答案:

答案 0 :(得分:2)

AddEventsCell实施prepareForReuse

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

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