【问题标题】:Save (in an array) content of dynamic cells (from a tableView), at runtime在运行时保存(在数组中)动态单元格(来自 tableView)的内容
【发布时间】:2013-08-01 11:34:56
【问题描述】:

如何选择(并将它们保存在数组中)tableViewController 的动态单元格的内容? 我的 tableView 由始终存在的 1 个部分组成,我可以在其中设置要在运行时显示的部分数量。 该设置由步进器控制。当我单击步进器上的“+”按钮时,表格中会添加一个包含三行的部分。 每行包含一个文本字段!考虑到运行时甚至可能有 1000 个部分,我如何选择这些行的内容并将它们保存在数组中? 代码如下:

- (void)viewDidLoad{
[super viewDidLoad];
self.arrDetails = [NSArray arrayWithObjects:@"Name",@"Surname",@"Fix",@"Role", nil];}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 + self.numberOfComponents;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        return 1;
    }


    else{
        return [self.arrDetails count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSString *CellIdentifier = @"cellStepper";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        UILabel *stepperLabel = (UILabel *)[cell viewWithTag:1];
        stepperLabel.text = [NSString stringWithFormat:@"%i",self.numberOfComponents];
        UIStepper *stepper = (UIStepper *)[cell viewWithTag:2];
        stepper.minimumValue = 0;
        stepper.maximumValue = 20;
        stepper.stepValue = 1;
        //stepper.wraps = YES;
        stepper.autorepeat = YES;
        stepper.continuous = YES;
        return cell;
    }
    NSString *CellIdentifier = @"cellDetail";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UITextField *cellLabelComponent = (UITextField *)[cell viewWithTag:3];
    cellLabelComponent.placeholder = self.arrDetails[indexPath.row];
    return cell;
}


- (IBAction)stepperClik:(UIStepper *)stepper{
    if (stepper.value == 0 && self.numberOfComponents == 0) {
        if (stepper.value > self.numberOfComponents) {
            self.numberOfComponents += 1;
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        else{
            return;
        }
    }
    if (stepper.value > self.numberOfComponents) {
        self.numberOfComponents += 1;
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else{
        self.numberOfComponents -= 1;
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

@end

【问题讨论】:

    标签: objective-c nsarray uitableview


    【解决方案1】:

    由于您在一个部分中有固定数量的单元格,并且可以使用self.numberOfComponents 访问该部分的数量,因此您可以为每个单元格构建一个 indexPath。

    NSMutableArray *savedTexts = [NSMutableArray alloc] init];
    for (int i = 0; i < self.numberOfComponents; i < 0)
    {
        NSMutableArray *component = [NSMutableArray alloc] init];
        for (int j = 0; j < [self.arrDetails count]; j < 0)
        {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i+1];
            UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];
            [component addObject: [cell viewWithTag:3].text];
        }
        [savedTexts addObject: component];
    }
    

    有了这个,您应该有一个组件数组,每个组件都包含其详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2014-03-22
      相关资源
      最近更新 更多