Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用类似于提醒应用程序的开关控制在表视图中添加行_Ios_Iphone_Objective C - Fatal编程技术网

Ios 使用类似于提醒应用程序的开关控制在表视图中添加行

Ios 使用类似于提醒应用程序的开关控制在表视图中添加行,ios,iphone,objective-c,Ios,Iphone,Objective C,在Apple提醒应用程序中,以及提醒的详细信息屏幕中, 当您打开控件“在某个位置提醒我”时,会添加一行“位置”(实际上是表视图单元格)。 我想在我的一个应用程序中做同样的事情,当一个开关控件被激活时,添加两个单元格。。。我该怎么做? 感谢您在处理UI更改的方法中提供的帮助,您可以使用-(void)reloadData重新加载整个表视图,或者(更好)使用-(void)insertRowsAtIndexPaths:(NSArray*)indexPaths with rowanimation:(uit

在Apple提醒应用程序中,以及提醒的详细信息屏幕中, 当您打开控件“在某个位置提醒我”时,会添加一行“位置”(实际上是表视图单元格)。 我想在我的一个应用程序中做同样的事情,当一个开关控件被激活时,添加两个单元格。。。我该怎么做?
感谢您在处理UI更改的方法中提供的帮助,您可以使用
-(void)reloadData
重新加载整个表视图,或者(更好)使用
-(void)insertRowsAtIndexPaths:(NSArray*)indexPaths with rowanimation:(uitableviewsrowanimation)动画

您可以在一个单元格中使用UISwitch作为附件视图。但是,在重用单元时可能会遇到问题。如果只需要一行开关,只需将UISwitch作为强属性添加到TableViewController中即可。您必须在初始化控制器时创建它:

self.locationSwitch = [[UISwitch alloc] init];
    [self.locationSwitch addTarget:self action:@selector(handleSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
然后,您可以将其用作
UITableViewCell
的附件视图:

cell.accessoryView = self.locationSwitch;
当开关值更改时,您必须添加(或删除)行:


还要记住更新数据源,以便
tableView:numberofrowsinssection
返回的值与
self.locationSwitch的值一致。请检查此项目


这可能对你有帮助。

可能是重复的谢谢,这正是我想要的。
-(void)handleSwitchValueChanged:(id)sender
{
    NSIndexPath* indexPath = // calculate your index path;
    if(self.locationSwitch.on) {
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  }
  else {    
    [self.tableView insertRowsAtIndexPaths:@[indexPath]   withRowAnimation:UITableViewRowAnimationAutomatic];
  }
}