gpt4 book ai didi

iphone - 如何使用 UISwitch 创建 UITableViewCell 并获取数据?

转载 作者:行者123 更新时间:2023-12-03 18:15:04 24 4
gpt4 key购买 nike

我有一个简单的问题。我创建了一个自定义 UITableViewCell,其中包含 Interface Builder 中的 UISwitch。

问题 1:更简单、更好的方法吗?问题 2:当在 UITableViewController 中使用时,获取数据的最佳方式是什么?在某些时候,我要么需要浏览表格并检查每个单元格的状态,要么在开关状态直接更改时发送一些反馈?

感谢您的帮助。

最佳答案

您只需在附件 View 中添加 UISwitch 即可。这是最简单的方法,更不用说它看起来“优雅”了。

然后,在您的表格 View Controller 代码中,您可以在每次切换开关时调用选择器,甚至可以通过在 Controller 中获取开关的当前状态来切换开关本身。

如果您想要代码示例,请告知。

---示例代码---

- (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;
}

然后,您可以拥有一种根据模型中的更改来更新开关的方法。您可以使用任何您想要的东西 - 委托(delegate)、通知、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];
}

}

关于iphone - 如何使用 UISwitch 创建 UITableViewCell 并获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4585840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com