ios - 具有不同对象的动态 UITableview

标签 ios objective-c uitableview

我有一个带有 UITextField、UIDatePicker、UIPickerView 自定义单元格的表格 View 。 在我的方法 cellForRowAtIndexPath 中,我为每一行设置了它的行为。 但是我意识到,当我点击顶部的第一行时,一切都很好,而不是当我向下滚动表格并更改底部的值时,例如,我在第 7 行输入的值插入到第 8 行,依此类推下线 实际上,当我滚动表格时,它会混淆行的索引 或者例如 il UITableViewCellAccessoryDisclosureIndicator 而不是在重新加载表时将其分配给第 9 行,表将其分配给第 2 行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    UITextField *text1 = (UITextField *)[cell viewWithTag:100];
    UILabel *text2 = (UILabel *)[cell viewWithTag:200];
    text1.text = [valuesArray objectAtIndex:indexPath.row];
    text2.text = [titlesArray objectAtIndex:indexPath.row];

    switch (indexPath.row) {
        case 0:  
            {
                text1.delegate = self;
                text1.tag = indexPath.row;
            }
            break;

        case 1: 
            {
                activityPicker = [[UIPickerView alloc] init];
                activityPicker.delegate = self;
                activityPicker.tag = indexPath.row;
                text1.delegate = self;
                text1.inputView  = activityPicker;
            }
            break;

        case 2: 
            {
                datePicker = [[UIDatePicker alloc] init];
                datePicker.datePickerMode = UIDatePickerModeDate;
                [datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
                text1.delegate = self;
                text1.inputView = datePicker;
                datePicker.tag = indexPath.row;
            }
            break;

        case 3: 
            {
                datePicker = [[UIDatePicker alloc] init];
                datePicker.datePickerMode = UIDatePickerModeTime;
                [datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
                text1.delegate = self;
                text1.inputView = datePicker;
                datePicker.tag = indexPath.row;
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"HH:mm"];
                [datePicker setDate: [dateFormatter dateFromString:text1.text]];
            }
            break;

        case 4:  
            {
                activityPicker = [[UIPickerView alloc] init];
                activityPicker.delegate = self;
                activityPicker.tag = indexPath.row;
                NSString *km = [text1.text substringToIndex:[text1.text length]-3];
                int numkm = [km intValue] - 1;
                [activityPicker selectRow:numkm inComponent:0 animated:YES];
                text1.delegate = self;
                text1.inputView  = activityPicker;
            }
            break;

        case 5: 
            {
                activityPicker = [[UIPickerView alloc] init];
                activityPicker.delegate = self;
                activityPicker.tag = indexPath.row;
                text1.delegate = self;
                text1.inputView = activityPicker;
            }
            break;

        case 6: 
            {
                activityPicker = [[UIPickerView alloc] init];
                activityPicker.delegate = self;
                activityPicker.tag = indexPath.row;
                if ([text1.text isEqualToString:[animalsArray objectAtIndex:0]])
                    [activityPicker selectRow:0 inComponent:0 animated:YES];
                else
                    [activityPicker selectRow:1 inComponent:0 animated:YES];

                text1.delegate = self;
                text1.inputView = activityPicker;
            }
            break;

        case 7:  
                text1.delegate = self;
                text1.tag = indexPath.row;
            }
            break;

        case 8:  
            {
                activityPicker = [[UIPickerView alloc] init];
                activityPicker.delegate = self;
                activityPicker.tag = indexPath.row;
                if ([text1.text isEqualToString:[privacyArray objectAtIndex:0]])
                    [activityPicker selectRow:0 inComponent:0 animated:YES];
                else
                    [activityPicker selectRow:1 inComponent:0 animated:YES];

                text1.delegate = self;
                text1.inputView = activityPicker;
            }
            break;

        case 9:  
        {
            text1.enabled = NO;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
            break;

        default:
            break;
    }

    return cell;
}

最佳答案

您的问题是对所有行使用相同的单元格标识符 "Cell"。这会导致您已经为特定 配置的单元格重新加载并再次用于另一行。

假设您只有这 10 个单元设计,您可以:

NSString*        identifier = [NSString stringWithFormat: @"Cell-%d", indexPath.row];
UITableViewCell* cell       = [tableView dequeueReusableCellWithIdentifier:identifier
                                                              forIndexPath:indexPath];

这样,dequeueReusableCellWithIdentifier 将只返回先前为当前 indexPath 创建的单元格。

关于ios - 具有不同对象的动态 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47621694/

相关文章:

ios - Objective-C 变量被设置为 null

ios - 如何在 viewDidLoad 之前初始化 UITableView?

ios - 请求错误代码为 999 的 AFNetworking

ios - CMSampleBuffer 到 Swift 中的字节数组

ios - 有时 ios MKMapView 在转到其他屏幕并返回到 MapScreen 时崩溃

c# - UITableView 标题和文本颜色

swift - 为什么我的 TableView scrollToRow 只能部分滚动?

ios - Braintree iOS SDK 4.0 - 重用添加新的支付 View Controller

ios - subview 不会成为第一响应者

objective-c - 拖动 vom NSTableView 的 NSDragOperation 为 -1