如何使用类似Alarm App iOS7的Switch编写UITableView? [英] How to code an UITableView with a Switch like the Alarm App iOS7?

查看:69
本文介绍了如何使用类似Alarm App iOS7的Switch编写UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 TableView ,就像iOS7上的Alarm App上的闹钟视图一样。视图显示警报,用户打开并通过开关关闭警报。

I would like to code a TableView like the alarm view on the Alarm App on iOS7. The view displays the alarms and the user turns ON and turns OFF the alarms with a switch.

我有2个视图:a TableView 控制器显示警报,查看控制器设置新警报。当用户点击View Controller上的ADD按钮时,它会在 TableView 上添加一个开关添加新警报。

I have 2 views : a TableView controller to display the alarms and a View controller to setup a new alarm. When the user taps on the ADD button on the View Controller, it adds the new alarm with a switch on the TableView.

我有一个 NSObject :XYZAlarm,其中包含2 NSString :alarm和descriptiveText。
我有一个 NSMutableArray :在 TableViewController 的.h文件中声明的Alarms。

I have an NSObject : "XYZAlarm" which contains 2 NSString : alarm and descriptiveText. I have an NSMutableArray : "Alarms" declared in the .h file of the TableViewController.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    XYZAlarm *alarm = [self.alarms objectAtIndex:indexPath.row];
    cell.textLabel.text = alarm.itemName;
    UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchview;
    return cell;
}

我将以 NSUserDefaults
关于开关,我有点迷失了。如何编写开关功能以启用或禁用警报?以及如何存储每个交换机的状态?

I will store the alarms with NSUserDefaults. I'm a little bit lost concerning the switch. How can I code the switch function to enable or disable the alarms ? And how to store the state of each switch ?

感谢您的帮助!

编辑:我找到了解决方案here 。谢谢大家!

EDIT : I found the solution here. Thanks everybody !

推荐答案

问题的核心是知道交换机何时发生变化,哪一行切换进入。您必须为每个开关提供一个 valueChanged 的操作方法,以便您可以了解用户何时移动开关。但是你怎么知道这是哪一行的开关呢?以下是我喜欢的方式:

The heart of the problem is knowing when the switch changes and which row the switch was in. You must give each switch an action method for its valueChanged so that you can learn when the user moves the switch. But how will you know which row's switch this is? Here's how I like to do it:

UIView* v = sender; // sender is the switch sending `valueChanged`
do {
    v = v.superview;
} while (![v isKindOfClass: [UITableViewCell class]]);
UITableViewCell* cell = (UITableViewCell*)v;
NSIndexPath* ip = [self.tableView indexPathForCell:cell];

现在您知道作为交换机超级视图的单元格的索引路径(部分和行)那改变了。所以现在你可以在你的模型中存储这个变化。

Now you know the index path (section and row) for the cell that is the superview of the switch that was changed. So now you can store this change in your model.

确保每个时间设置开关的状态 cellForRowAtIndexPath:调用,因为单元格被重用。

Be sure to set the switch's state every time cellForRowAtIndexPath: is called, because the cells are reused.

这篇关于如何使用类似Alarm App iOS7的Switch编写UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆