【发布时间】:2016-01-07 08:53:10
【问题描述】:
我有一个表格视图,其中根据遇到的数据类型为许多行创建/重用自定义单元格(即对于 Date 类型,单元格的文本字段从 datePicker 中选择选定的日期,对于 enum 类型,它选择选定的来自下拉选择器的数据)。现在页脚有一个按钮,需要从每个单元格中提取数据并对其进行处理以显示更多视图控制器。
由于单元格中的数据在选择后(从日期选择器或下拉菜单或键盘)显示良好,但我们如何在页脚按钮方法中捕获这些数据。
这里是代码:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [valueArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *values = [valueArray objectAtIndex:indexPath.row];
NSString *titles = [titleArray objectAtIndex:indexPath.row];
if([titles isEqualToString:@“enumType”]){
cell.headerInfo.hidden = true;
cell.valueObtained.hidden=true;
UITextField * scheduelText = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 130, 30)];
NSMutableArray* enumArray = [[NSMutableArray alloc] init];
[enumArray addObject:@"BUSINESS"];
[enumArray addObject:@"CUSTODIAL"];
[enumArray addObject:@"INDIVIDUAL"];
downPicker = [[DownPicker alloc] initWithTextField:scheduelText enumArray];
scheduelText.text = downPicker.text;
[cell addSubview:scheduelText];
}
else if([titles isEqualToString:@"DateType”]){
cell.headerInfo.hidden = true;
cell.valueObtained.hidden=true;
UITextField *dateText = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 130, 30)];
dateText.placeholder=@"Date";
dateText.inputView = self.datePicker;
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *doItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dateDoneButton)];
[toolBar setItems:@[doItem]];
dateText.inputAccessoryView = toolBar;
[cell addSubview:dateText];
}
else if([titles isEqualToString:@“TextType”]){
cell.headerInfo.hidden = true;
cell.valueObtained.hidden=true;
UITextField * accountText = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 130, 30)];
accountText.returnKeyType = UIReturnKeyDone;
accountText.delegate = self;
accountText.placeholder=@"Account Name";
[accountText setTextColor:[UIColor blackColor]];
[accountText setBorderStyle:UITextBorderStyleNone];
[cell addSubview:accountText];
}
else{
cell.valueObtained.text = values;
cell.headerInfo.text = titles;
}
return cell;
}
- (void)submitButtonPressed:(id)sender
{
//Need to capture the cell data here ?
}
请帮忙。
【问题讨论】:
-
我真的建议为此创建自定义单元格类,这样您就可以为其创建一个委托或传递代码块,否则您必须创建标签(不推荐)或使用@987654322 将点转换为单元格@
-
有两个选项:TAG 和 DELEGATE。
标签: ios objective-c uitableview