iOS 6 UITextField里面的UITableViewCell与部分获取滚动上的错误数据
所以基本上,我通过滚动我的TableView(每件事都混在一起)在每个textField上得到错误的数据。我知道,这是因为UITableView中可重用单元的优化优化,所以indexpath不断变化,但我仍然不知道如何解决这个问题。iOS 6 UITextField里面的UITableViewCell与部分获取滚动上的错误数据
在同一个单元格中有一个UILabel我通过在每个标签视图中添加一个特定的标签来解决这个问题,所以tableview知道哪些数据将在视图上返回,但由于某些原因,这对我的文本视图来说是不可能的。
这里是我的代码:
#define DESCRIPTION_TAG 10
#define TEXTFIELD_TAG 11
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Description Cell";
// Intatiating my own TableViewCell
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell){
cell = [[CustomLocationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.rowText = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG]; // returning view with unique tag
cell.rowTextField = (UITextField *)[cell viewWithTag:TEXTFIELD_TAG]; // NOT WORKING!!! Data get mix up
cell.rowTextField.delegate = cell; // sending the delegate to custom class cell
cell.delegate = self; // make connection with my @protocol
switch (indexPath.section) {
case 0:
cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row];
cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
break;
case 1:
cell.rowText.text = [self.rooms objectAtIndex:indexPath.row];
cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row];
break;
default:
break;
}
return cell;
}
任何帮助表示赞赏。
这里是我的数据源的更新: 在我CustomTableViewCell我有网点为我的UILabel,同时也是UITextField
// UITableView DataSource
- (NSArray *)sections
{
if (!_sections){
_sections = [[NSArray alloc] initWithObjects:@"First Description", @"Second Descriptions", nil];
}
return _sections;
}
- (NSMutableArray *)rooms
{
if (!_rooms){
_rooms = [[NSMutableArray alloc] init];
}
return _rooms;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return [self.descriptions count];
break;
case 1:
return [self.rooms count];
break;
default:
break;
}
return 0;
}
我不知道你要完成什么,但这里是一个使用占位符文本的标签和文本字段的示例。您会注意到,在cellForRowAtIndexPath中,我将文本和占位符都设置为nil,以便在重用单元时,它没有错误的文本。另外,在执行文本字段时,我检查数组是否在该索引处具有占位符文本,如果是,则重置占位符,如果不是,则重置文本。
为了使文本字段的委托方法正常工作,每个文本字段都需要一个唯一的标签(不仅仅是一个标签和另一个文本字段),我将其设置为(indexPath.row + 1000 * indexPath。部分)。
#import "TableController.h"
#import "CustomLocationCell.h"
@interface TableController()
@property (strong,nonatomic) NSArray *descriptions;
@property (strong,nonatomic) NSMutableArray *descriptionsPlaceholder;
@property (strong,nonatomic) NSArray *rooms;
@property (strong,nonatomic) NSMutableArray *contentPlaceholder;
@end
@implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
self.descriptions = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight"];
self.descriptionsPlaceholder = [@[@"Placeholder1",@"Placeholder2",@"Placeholder3",@"Placeholder4",@"Placeholder5",@"Placeholder6",@"Placeholder7",@"Placeholder8"] mutableCopy];
self.rooms = @[@"Room1", @"Room2", @"Room3", @"Room4",@"Room5", @"Room6", @"Room7", @"Room8"];
self.contentPlaceholder = [@[@"ContentPH1",@"ContentPH2",@"ContentPH3",@"ContentPH4",@"ContentPH5",@"ContentPH6",@"ContentPH7",@"ContentPH8"] mutableCopy];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.descriptions.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Description Cell";
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.rowTextField.delegate = self;
cell.rowTextField.tag = indexPath.row + 1000*indexPath.section;
cell.rowTextField.placeholder = nil;
cell.rowTextField.text = nil;
switch (indexPath.section) {
case 0:
cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row];
if ([self.descriptionsPlaceholder[indexPath.row] hasPrefix:@"Placeholder"]) {
cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
}else{
cell.rowTextField.text = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
}
break;
case 1:
cell.rowText.text = [self.rooms objectAtIndex:indexPath.row];
if ([self.contentPlaceholder[indexPath.row] hasPrefix:@"ContentPH"]) {
cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row];
}else{
cell.rowTextField.text = [self.contentPlaceholder objectAtIndex:indexPath.row];
}
break;
default:
break;
}
return cell;
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag >= 1000 && ![textField.text isEqualToString: @""]) {
[self.contentPlaceholder replaceObjectAtIndex:textField.tag - 1000 withObject:textField.text];
}else if (textField.tag < 1000 && ![textField.text isEqualToString: @""]) {
[self.descriptionsPlaceholder replaceObjectAtIndex:textField.tag withObject:textField.text];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
非常感谢@rdelmar,它很棒。 对不起,我的坏解释顺便说一句。 – Pompeyo 2013-04-28 11:19:52
设置占位符不改变显示的文本。 – 2013-04-27 15:22:58
你可以试一下[cell.contentView viewWithTag:TEXTFIELD_TAG]; – Anupdas 2013-04-27 15:23:44
感谢您的回应! @ A-Live,我认为这不是问题,因为当用户编写一些文本并滚动表格时,这个新文本会混淆。 – Pompeyo 2013-04-27 16:30:37