我的应用程序中有一个表单,它存在于带有自定义单元格的UITableView中。这些单元可以包含UITextField、UISegmentedControl或UISwitch。我就是这样设置这个的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewInner cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DetailTableViewCell *cell;
static NSString *MyIdentifier = @"MyIdentifier";
DetailTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[DetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
[cell setTextField:@"John Appleseed"];
// or
[cell setSegment];
[cell setSegmentIndex:1];
// or
[cell setSwitch];
[cell setSwitchEnabled:YES];
return cell;
}现在,当用户点击保存按钮时,我需要获取所有这些信息并使用它来init一个模型,如下所示:
[[Restaurant alloc] initWithName:@"Name here" withNotifications:1 withFrequency:1 withDate:@"Date here" andWithDistance:@"Distance here"];在我的模型中,将所有这些输入转换为数据的最佳和最干净的方法是什么?我觉得把所有的细胞都循环起来有点过头了。
发布于 2016-03-12 17:39:09
就像在所有细胞上循环一样有点过了头
它不仅是过分的,它是完全错误的。数据不是活在细胞里,而是活在数据中。模型,视图,控制器;单元就是视图!它的工作是表示模型(数据)。因此,应该没有什么可循环的;您应该已经将数据作为数据。
现在,当用户点击“保存”按钮时,我需要获取所有这些信息。
实际上,我要做的是在用户进行更改时捕获信息。为文本字段、开关或分段控制提供一个控制操作--目标,以便向您发送一条消息,告诉您发生了什么事情(例如,开关值发生了变化,文本被编辑,等等),然后立即捕获数据。
唯一的问题是:我收到了来自控件的一条消息:它在表的哪一行?要找出答案,请将层次结构从控件上移至单元格,然后询问表该单元格代表的行是什么:
UIView* v = sender; // the control
do {
v = v.superview;
} while (![v isKindOfClass: [UITableViewCell class]]);
UITableViewCell* cell = (UITableViewCell*)v;
NSIndexPath* ip = [self.tableView indexPathForCell:cell];https://stackoverflow.com/questions/35960985
复制相似问题