我正在尝试实现基于UITableview的应用程序。在我的tableView中,它们是10节,每个节都有一行。
我要实现每个部分具有不同类型的ContentView(1-8个相同的ContentView第9部分不同的ContentView)。我为此编写了此代码。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"Cell1";
    static NSString *CellIdentifier2 = @"Cell2";
    UITextField *textField;
    UITextView *textView;
    NSUInteger section=[indexPath section];
    if(section == 9){
        UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
        //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if(cell==nil){
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
            textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
            [textView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor
                                          ]];
            [textView setTag:([indexPath section]+100)];
            [cell.contentView addSubview:textView];
        }else{
            textView=(UITextView*)[cell.contentView viewWithTag:([indexPath section]+100)];
        }
        return cell;
    }else {
        UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
       // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if(cell==nil){
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
            textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
            [textField setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
            [textField setTag:([indexPath section]+100)];
            [cell.contentView addSubview:textField];
        }else{
            textField=(UITextField*)[cell.contentView viewWithTag:([indexPath section]+100)];

        }

        return cell;
    }


    return nil;

}


我的问题是:
1.在UITextField / UITextView中键入一些内容后,我在UITableView中滚动。那时,除了最后一个单元格数据,所有UITableViewCellUITextField / UITextView)中的数据都丢失了。
2.如果我创建单元格

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


代替

 UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];


数据将重复。我怎么能克服这个问题?

最佳答案

这行:

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];


永远不要出现在您的数据源cellForRowAtIndexPath方法中。

除此之外,您的代码还可以,只是您没有在任何地方设置文本字段值。您需要一个模型(例如10个文本字段值的字符串数组)。编辑文本字段时,应该更新此模型,并在上述方法中,将值复制回模型之外并复制到文本字段的text属性中:

textfield.text = [self.modelArray objectAtIndex:indexPath.section];

关于iphone - UITableView滚动时,UITableViewCell上的数据丢失?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9835091/

10-13 03:55
查看更多