UITableView不重用单元格,这样做很好的理由吗? [英] UITableView don't reuse cells, good case to do this?

查看:75
本文介绍了UITableView不重用单元格,这样做很好的理由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UITableView中有5个单元格.每个都有一个UITextField作为子视图,用户将在其中输入数据.如果我确实使用单元格重用,则将单元格滚动到视图之外时,文本字段将被清除.我不想处理这个.有没有一种方法可以不重用单元格,这样我就不会遇到这个问题,如果可以,怎么办?

I have 5 cells in a UITableView. Each has a UITextField as a subview, where the user will input data. If I DO use cell reuse, the textfield gets cleared if the cells are scrolled out of view. I don't want to have to deal with this. Is there a way to NOT reuse cells so that I don't have this issue, if so, how?

这是个坏主意吗?

推荐答案

我在其中一个应用程序中具有相同的功能,并且使用下面的代码来完成此操作,但我从未遇到过此类问题.

I have same feature in one of my apps and I used below code to accomplish that and I never had this kind of problem.

首先,您需要将所有textField值临时存储在Array中.像这样制作数组.

First of all you need to store all your textField value temporary in Array. Make array like this.

arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],nil];

然后给所有textField标签= indexPath.row;

Then Give all textField tag = indexPath.row;

之后,您需要在以下两种方法中替换textField值.

After that You need to replace textField value in below two methods.

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}

最后,您需要在cellForRowAtIndexPath数据源方法中设置该值.这样,每当用户滚动tableview时,它都会从temp数组中设置先前的值.这样.

At Last You need to set that value in cellForRowAtIndexPath datasource Method. So that whenever user scroll tableview it set previous value from temp array. Like this.

cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row];

可能我忘记了一些粘贴在此处的代码.因此,如果您有任何问题,请告诉我.

It might possible I forgot some of the code to paste here. So if you have any problem please let me know.

这篇关于UITableView不重用单元格,这样做很好的理由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆