带有部分的 UITableViewCell 中的 iOS 6 UITextField 在滚动时得到错误的数据

标签 ios uitableview scroll

所以基本上我通过滚动我的 TableView 在每个 textField 上得到错误的数据(一切都混淆了)。我知道,这是因为 UITableView 中可重用单元格的出色优化,所以索引路径不断变化,但我仍然不知道如何解决这个问题。

在同一个单元格中有一个 UILabel 我通过在每个标签 View 中添加一个特定的标签解决了这个问题,因此 tableview 知道哪些数据将在 View 上返回,但由于某些原因,这对于我的文本字段 View 是不可能的。

这是我的代码:

#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 的 socket

// 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,这样当一个单元格被重复使用时,它就不会包含错误的文本。此外,在处理文本字段时,我会检查数组在该索引处是否有占位符文本,如果有,我会重置占位符,如果没有,我会重置文本。

为了让文本字段委托(delegate)方法正常工作,每个文本字段都需要一个唯一的标签(不仅仅是一个标签用于标签,另一个用于文本字段),我将其设置为 (indexPath.row + 1000*indexPath.section ).

#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;
}

关于带有部分的 UITableViewCell 中的 iOS 6 UITextField 在滚动时得到错误的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252878/

相关文章:

iphone - 网址为:(空),但请求字符串不为空

iphone - 如何获取iOS 6中用户的联系人列表?

ios - 向上或向下滚动 UITableview 时更改值

ios - 如何在 swift 4 中重新排序单元格后保存 tableView 顺序

ios - 在带有分页的 ScrollView 中拥有多个 tableView 会使 tableHeaderView 的 searchBar 不可触摸

android - 如何在android中的集中布局内制作textview选框

javascript - 将css添加到head并使用javascript动态刷新它

ios - 不在 UITextField 自定义子类中调用的委托(delegate)方法

ios - 观察自定义单元格

Android:ScrollView 和绘图缓存?