我是iOS编程的新手,我想保存在动态表格单元格内放置的文本框内输入的文本,其中表格单元格内容就像手风琴一样。
在视图控制器表视图中使用3个动态原型单元格,每个单元格分别命名为header,regular和selected,并在视图控制器中使用以下代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = nil;
switch (indexPath.row) {
case 0:
{
cell = [tableView dequeueReusableCellWithIdentifier:@"Header"];
}
break;
default:
{
NSArray * innerData = [self.data objectAtIndex:indexPath.row - 1];
if(indexPath.row == self.selectedIndex)
{
SelectedCell * tmpCell = (SelectedCell *)[tableView dequeueReusableCellWithIdentifier:@"Selected"];
[tmpCell.label1 setText:[NSString stringWithFormat:@"%@", [innerData objectAtIndex:0]]];
cell = tmpCell;
tmpCell.textLabel.numberOfLines=2;
}
else
{
RegularCell * tmpCell = (RegularCell *)[tableView dequeueReusableCellWithIdentifier:@"Regular"];
[tmpCell.label1 setText:[NSString stringWithFormat:@"%@", [innerData objectAtIndex:0]]];
cell = tmpCell;
tmpCell.textLabel.numberOfLines=2;
}
}
break;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0)
return;
if(indexPath.row != self.selectedIndex && self.selectedIndex > 0)
{
// Selected different row while there is another selected row
int oldIndex = self.selectedIndex;
self.selectedIndex = indexPath.row;
NSArray * innerData = [self.data objectAtIndex:oldIndex - 1];
[self.data removeObjectAtIndex:oldIndex - 1];
NSIndexPath * deselectedIndexPath = [NSIndexPath indexPathForRow:oldIndex inSection:0];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deselectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.0 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
[self.data insertObject:innerData atIndex:deselectedIndexPath.row - 1];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:deselectedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView reloadData];
NSArray * innerData = [self.data objectAtIndex:indexPath.row - 1];
[self.data removeObjectAtIndex:indexPath.row - 1];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
[self.data insertObject:innerData atIndex:indexPath.row - 1];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView reloadData];
});
});
}
else
{
if(indexPath.row == self.selectedIndex) // selected active row -> close
self.selectedIndex = -1;
else // selected inactive row -> open
self.selectedIndex = indexPath.row;
NSArray * innerData = [self.data objectAtIndex:indexPath.row - 1];
[self.data removeObjectAtIndex:indexPath.row - 1];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
[self.data insertObject:innerData atIndex:indexPath.row - 1];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView reloadData];
});
}
}
现在我想存储在特定于该单元格的每个字段中输入的文本我搜索了很多教程但是我找不到它
请尽快为我提供合适的解决方案
提前致谢