UITablView中的自定义单元格不保存用户输入

时间:2017-11-08 17:52:49

标签: ios objective-c uitableview nsmutablearray

我上传了正在进行的应用的快速视频

https://vimeo.com/241910642

正如您所看到的,我可以按照设计添加和删除tableview中的行,但是当保存用户输入的数字(数据)时,应用程序会一直调用(null)...我在亏本......有什么想法吗?在我的一些读物中,我想知道自定义单元格是否继续将其自身重写为“空”...在viewDidLoad ...

以下是一些可能有用的代码......

创建单元格/行...

- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

SodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
    cell = [[SodTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}


if(indexPath.row >= tabledata.count && [self isEditing]){

    cell.areaNumber.text = @"new area";


}else{ // this tableview is showing table (not in edit mode)


    cell.areaNumber.text = [tabledata objectAtIndex:indexPath.row];

    NSUserDefaults *save = [NSUserDefaults standardUserDefaults];
    [save setObject:tabledata forKey:@"tablerow_area_input_by_user"];
    [save synchronize];
    NSLog(@"This number is from cell creation code %@",[save valueForKey:@"tablerow_area_input_by_user"]);

}

return cell;
}

其中 cell.areaNumber.text 应该是用户可以输入数据的区域,当保存到NSMutableArray tabledata 时。此 IS 保存了行的创建和删除,但似乎 NOT 保存用户输入的数据...

所以,我想知道在退出和进入应用程序时,是否使用我的代码来保存数据或用于显示数据的代码......

以下是我的viewDidLoad的一部分...

- (void)viewDidLoad
{
[super viewDidLoad];

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"+ / -" style: UIBarButtonItemStylePlain target:self action:@selector(addORDeleteRows)];[self.navigationItem setRightBarButtonItem:addButton];


tabledata = [[NSMutableArray alloc] init];

if([tabledata count]==0){
    //Your array is empty so you have to call it with @"yourKey"
    tabledata=[[[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"tablerow_area_input_by_user"]]mutableCopy];
    // not sure if I need to add "mutableCopy" to end or not

}

numberOfSection = 1;

[myTable reloadData];

这就是我试图用这个数据保存数据(用FUTURE意图)...

-(IBAction)save_area:(id)sender {

{
        UITableView *tableView = self.myTable;
        NSInteger sections = tableView.numberOfSections;
        NSMutableArray *cells = [[NSMutableArray alloc]  init];
        for (int section = 0; section < sections; section++) {
            NSInteger rows =  [tableView numberOfRowsInSection:section];
            for (int row = 0; row < rows; row++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
                UITableViewCell *cell = [self.myTable cellForRowAtIndexPath:indexPath];//**here, for those cells not in current screen, cell is nil**
                [cells addObject:cell];

                NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
                [save1 setObject:_areaArrayDimension forKey:@"sod_area_input_by_user"];
                [save1 synchronize];
                NSLog(@"This number is from area input %@",[save1 valueForKey:@"sod_area_input_by_user"]);



            }
        }
    }

}

您注意到我现在将新的NSMutableArray保存到_areaArrayDimension,以便不覆盖用于创建或删除tabledata中的行的NSMutableArray。不确定,但控制台指示

2017-11-08 12:50:12.309723-0500 landscapepro2[5150:608237] This number is from area input (null)
2017-11-08 12:50:12.309990-0500 landscapepro2[5150:608237] This number is from area input (null)
2017-11-08 12:50:12.310183-0500 landscapepro2[5150:608237] This number is from area input (null)
2017-11-08 12:50:12.310642-0500 landscapepro2[5150:608237] This number is from area input (null)
2017-11-08 12:50:12.311125-0500 landscapepro2[5150:608237] This number is from area input (null)

很抱歉这个冗长的问题,但是我被困在这个项目上,一旦弄明白,我需要在保存后对这些数字做点什么。

感谢您抽出时间审核......

2 个答案:

答案 0 :(得分:0)

所以几乎解决了......我能够在UITextField中保存一个值并将其保存到数组中并显示它...但是,如果你在这里查看视频on vimeo showing the result

你会注意到我所指的行为。它只保存表格中最后一行的最后一行,然后当它从视图返回时,它出现在表格的第一行......我必须要包含一些“额外的部分”,上面写着“将我添加到下一行”数组中的行“:)我认为它可能与我在 textFieldDidChange 中的代码有关(见下文)您还可以在视频中查看我的控制台以供参考...以下是代码使用textFieldDidChange ...

保存值
head -1 Outbox/ICCP/PCICCP_AnalogPoint_DMS.csv | awk -F"," '{for(i=1;i<NF;i++) if($i=="AnalogPointRef") {print i}}'
9

以下是显示值

的代码
- (void)textFieldDidChange :(UITextField *)theTextField
{
NSIndexPath *indexPath = [myTable indexPathForSelectedRow];
[tabledata removeObjectAtIndex:indexPath.row];
[tabledata insertObject:theTextField.text atIndex:indexPath.row];
}

我还创建了一个按钮来保存此代码中的所有数字......

- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

SodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
    cell = [[SodTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

//if number of rows is greater than the total number of rows in the data set and this view is in editing mode.
//Initialize the cell for "Add Row"
//there will be an extra row once SetEditing: is called
if(indexPath.row >= tabledata.count && [self isEditing]){

    cell.areaNumber.text = @"new area";


}else{ // this tableview is showing table (not in edit mode)

    //Using your data source array to get your data for each cell
    //[cell addSubview:cell.areaNumber];

    [cell.areaNumber addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    cell.areaNumber.delegate = self;

    cell.areaNumber.text = [tabledata objectAtIndex:indexPath.row];

}

return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(SodTableCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

// Configure the cell...
UILabel*label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2.5, 0, self.view.bounds.size.width, 44)];

NSString*text = [NSString stringWithFormat:@"Area %ld",(long)indexPath.row+1];
[label setText:text];
[label setTextColor: [UIColor colorWithRed:0.41 green:0.33 blue:0.25 alpha:1.0]];
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:14.0f];
[cell.contentView addSubview: label];

NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
[save1 setObject:tabledata forKey:@"tablerow_area_input_by_user"];
[save1 synchronize];
NSLog(@"This number is from cell configuration %@",[save1 valueForKey:@"tablerow_area_input_by_user"]);
}

}

答案 1 :(得分:0)

...解决

以下是与我一起使用的代码&#34; save&#34;按钮动作......基本上我需要将自定义单元格识别为函数的一部分...

-(IBAction)save_area:(id)sender {

{
        UITableView *tableView = self.myTable;
        NSInteger sections = tableView.numberOfSections;
        NSMutableArray *cells = [[NSMutableArray alloc]  init];
        for (int section = 0; section < sections; section++) {
            NSInteger rows =  [tableView numberOfRowsInSection:section];
            for (int row = 0; row < rows; row++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
                UITableViewCell *cell = [self.myTable cellForRowAtIndexPath:indexPath];//**here, for those cells not in current screen, cell is nil**
                [cells addObject:cell];

                SodTableCell *cell2 = (SodTableCell *) [tableView cellForRowAtIndexPath:indexPath];

                [tabledata removeObjectAtIndex:indexPath.row];
                [tabledata insertObject:cell2.areaNumber.text atIndex:indexPath.row];

                NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
                [save1 setObject:tabledata forKey:@"tablerow_area_input_by_user"];
                [save1 synchronize];
                NSLog(@"This number is from save button %@",[save1 valueForKey:@"tablerow_area_input_by_user"]);

            }
        }
    }
}