ios - 为设置创建一个 UITableView。

标签 ios uitableview architecture

在写一个表格来呈现和组织应用程序设置区域时,我总是觉得有点“困惑”。我认为更好的方法是使用静态表,但我发现自定义它非常困难(特别是因为我无法自定义分组 View 单元格)。

所以我经常会得到一个带有数据源的公用表。我的疑问与应该直接显示选项信息的单元格有关。

假设我需要两种细胞。

  • 显示开关的单元格
  • 显示选项
  • 的当前值的单元格

    enter image description here

    我的疑问主要是:
  • 设置细胞的最佳方法是什么?我应该创建一个子类并在 cellForItemAtIndex 期间只显示或隐藏标签或开关吗?过程?
  • 与细胞相互作用的最佳方式是什么?当用户更改开关的值时......我如何设置对该开关的引用以及包含它的正确单元格?
  • 数据源呢?目前我实现了一个开关,并根据所需的索引设置了单元格

  • 您能否分享一些关于如何实现设置的想法/代码示例?

    最佳答案

    这个答案基于我在自己的应用程序中所做的事情。

  • 除非我想做一些“真正”定制的事情,否则我不会继承 UITableViewCell。我只会使用标准的 UITableViewCell,创建一个 UISwitch 控件或标签,具体取决于您要显示的内容,并将其添加为单元格的附件。例如:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString * cellIdentifier = @"Cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (!cell)
            cell = [[TSOptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
        if (indexPath.row == 0) /* or whatever index you want to set the switch or label at */
        {
            UISwitch * switch = [[UISwitch alloc] initWithFrame:frame];
            [switch addTarget:self action:@selector(toggleSwitch:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
            /* create your switch and add it's target and action (method that's called when it is toggled) */
            // Set the value of the switch 
            switch = [[NSUserDefaults standardUserDefaults] boolForKey:@"key"];
    
            [cell setAccessoryView:switch];
        }
    }
    
  • 当用户与开关交互时,将调用您添加为上述开关的“操作”的方法。例如,在你的代码中的某个地方,你会有这个:
    -(void)toggleSwitch:(id)sender
     {
         /* handle user interaction somehow */
     }
    
  • 这就是我目前在应用商店中使用我的应用所做的事情。我使用 switch 语句来检查 indexPath.row 与我希望显示开关的位置的索引,所以我认为这是合适的。
  • 关于ios - 为设置创建一个 UITableView。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24311393/

    相关文章:

    objective-c - 使用 AVAudioPlayer 框架完成播放后,如何再次播放相同的声音?

    uitableview - missViewControllerAnimated 卡住应用程序

    objective-c - 同一 View 中的两个 UITableView

    ios - SWTableViewCell 以编程方式滑动

    ios - 代码签名问题 Xcode 7.2

    ios - 自调整 UITableView 单元格的大小不正确

    architecture - 榆树 0.17 : How to subscribe to sibling/nested component changes

    database - 微服务:每个实例或每个微服务的数据源?

    assembly - 为什么有些架构没有 MOV?

    objective-c - 更改 UIWebView 文本的大小