【问题标题】:Creating a UITableView for settings.为设置创建一个 UITableView。
【发布时间】:2014-08-10 06:35:12
【问题描述】:

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

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

假设我需要两种细胞。

  1. 显示开关的单元格

  2. 显示选项当前值的单元格

我的疑惑主要是:

  1. 设置单元的最佳方法是什么?我应该创建一个子类并在cellForItemAtIndex 过程中只显示或隐藏标签或开关吗?

  2. 与细胞相互作用的最佳方式是什么?当用户更改开关的值时......我如何设置对该开关的引用以及包含它的正确单元格?

  3. 数据源呢?目前我实现了一个开关,并根据所需的索引设置了单元格

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

【问题讨论】:

  • 作为一个小提示少回答:要识别单元格,您可以在 cellForRowAtIndexPath 中为单元格层设置名称标签:cell.layer.name = @"YourIdentifier"。稍后在 didSelectRow 中阅读并采取相应措施。

标签: ios uitableview architecture


【解决方案1】:

此答案基于我在自己的应用中所做的事情。

  1. 除非我想做一些“真正”定制的事情,否则我不会继承 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];
        }
    }
    
  2. 当用户与开关交互时,将调用您为上述开关添加为“操作”的方法。例如,在你的代码中的某个地方,你会有这样的:

    -(void)toggleSwitch:(id)sender
     {
         /* handle user interaction somehow */
     }
    
  3. 这是我目前在应用商店中使用我的应用所做的。我使用 switch 语句来检查 indexPath.row 与我希望显示开关的位置的索引,所以我认为这是合适的。

【讨论】:

  • 关于开关:您应该有很多带有开关的单元,而不仅仅是一个。因此,当调用 cellForItemAtIndexPath 时,您应该使用一些临时存储的值相应地设置开关的当前状态(这就是我现在所做的......但听起来像是一种解决方法)
  • @MatterGoal 正确。这就是我目前所做的。首次启动应用程序时,我在 NSUserDefaults 中为我的开关和其他设置设置了默认值。所以我做了这样的事情 switch = [[NSUserDefaults standardUserDefaults] boolForKey:@"timerEnabled"];
【解决方案2】:

另一种方法是在情节提要中创建两个原型单元格,具有不同的标识符,并在

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

您应该使用所需索引处的每个单元格。 关于 cellForRowAtIndexPath 中的第二个问题,您应该将开关的当前状态保存在 NSMutableArray 中。

你为每个单元格设置一个标签

- (void)toggleSwitch:(id)sender {
    NSInteger currentIndex;
    currentIndex = [sender superview].tag;
  [yourMutableArray replaceObjectAtIndex:currentIndex withObject:![NSNumber numberWithBool:[yourMutableArray objectAtIndex:currentIndex]BoolValue];

}

当然,在你的 cellForRow 中,你可以像这样设置数组的值:

[yourMutableArray insertObject:[NSNumber numberWithBool:[mySwitch isOn] atIndex:indexPath.row];

希望这是您正在搜索的内容

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多