我有一个简单的问题。我创建了一个自定义 UITableViewCell,其中包含 Interface Builder 中的 UISwitch。
问题1:更简单,更好的方法吗?问题 2:在 UITableViewController 中使用时,获取数据的最佳方式是什么?在某些时候,我要么需要浏览表格并检查每个单元格的状态,要么在开关状态直接改变时发送一些反馈?
谢谢您的帮助。
我有一个简单的问题。我创建了一个自定义 UITableViewCell,其中包含 Interface Builder 中的 UISwitch。
问题1:更简单,更好的方法吗?问题 2:在 UITableViewController 中使用时,获取数据的最佳方式是什么?在某些时候,我要么需要浏览表格并检查每个单元格的状态,要么在开关状态直接改变时发送一些反馈?
谢谢您的帮助。
您可以UISwitch
在附件视图中添加。这是最简单的方法,更不用说它看起来“优雅”了。
然后,在您的 tableview 控制器代码中,您可以在每次切换开关时调用选择器,甚至可以通过在控制器中获取开关的当前状态来切换开关本身。
如果您需要代码示例,请告知。
---示例代码---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//add a switch
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview release];
}
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}
然后,您可以使用一种方法来根据模型中的更改更新开关。你可以使用任何你想要的东西——委托、通知、KVO 等等。
例子:
- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UISwitch *switchView = (UISwitch *)cell.accessoryView;
if ([switchView isOn]) {
[switchView setOn:NO animated:YES];
} else {
[switchView setOn:YES animated:YES];
}
}
Switch.tag = indexPath.row;
[Switch addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
- (void)updateSwitchAtIndexPath:(UISwitch *)aswitch{
FFLog(@"%i",aswitch.tag);
}
谢谢。我没有您的 indexPath,但您可以使用如下标签识别单元格或对象:
if (indexPath.row == 0) {
cell.textLabel.text = @"Twitter";
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
switchview.tag = 111;
//switchview.selected = 0;
cell.accessoryView = switchview;
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
//cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
//[switchview release];
}
- (void)updateSwitchAtIndexPath {
for (UITableViewCell * cell in tblView.subviews) {
for (UISwitch *swch in cell.subviews) {
UISwitch *switchView = (UISwitch *)cell.accessoryView;
if (switchView.tag == 111) {
if ([switchView isOn]) {
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ON");
}else{
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OFF");
}
}
if (switchView.tag == 222) {
if ([switchView isOn]) {
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ON");
}else{
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OFF");
}
}
}
}
}
两种选择:
1) 使用指向数据模型的指针初始化单元格。让单元实现开关的委托回调。当开关更改时,使用指向数据模型的指针来反映新设置。或者,使用指向您的视图控制器的指针初始化单元格。让单元实现切换委托方法,然后使用适当的单元上下文信息回调到您的视图控制器。
2)让您的视图控制器实现切换委托回调。当开关改变时,确定它是哪个开关/单元(可能必须将开关的“标签”属性设置为行号或其他东西)并在视图控制器的上下文中处理更新。
要获取UISwitch
Swift 3 和 Swift 4 中的值,您可以向其中添加目标:
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
...
cell.uiSwitch.tag = 100
cell.uiSwitch.addTarget(self, action: #selector(ViewController.switchAtValueChanged(switchFromTableViewCell:)), for: UIControlEvents.valueChanged)
...
}
你可以得到这样的值:
@objc func switchAtValueChanged(uiSwitch: UISwitch) {
if uiSwitch.tag == 100 {
let value = uiSwitch.isOn
}
}