所以基本上,我通过滚动我的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;
}
设置占位符不改变显示的文本。 – 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