UITableViewCell中的iOS 6 UITextField与部分在滚动时获取错误的数据

时间:2013-04-27 13:49:26

标签: ios uitableview scroll

所以基本上我通过滚动我的TableView来获取每个textField上的错误数据(Everything is mixed up)。我知道,这是因为UITableView中可重用单元的优秀优化,所以索引路径不断变化,但我仍然不知道如何解决这个问题。

在同一个单元格中有一个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;
}

1 个答案:

答案 0 :(得分:2)

我不确定您要完成的是什么,但这是一个使用占位符文本的标签和文本字段的示例。您会注意到在cellForRowAtIndexPath中,我将text和placeholder都设置为nil,以便在重用单元格时,它没有错误的文本。此外,在执行文本字段时,我检查数组是否在该索引处有占位符文本,如果是,则重置占位符,如果不是,则重置文本。

为了使文本字段委托方法正常工作,每个文本字段都需要一个唯一的标记(不只是一个标签用于标签,另一个用于文本字段)我设置等于(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;
}