【问题标题】:Programmatically embed multiple UISlider and UILabel controls in UITableViewCells以编程方式在 UITableViewCells 中嵌入多个 UISlider 和 UILabel 控件
【发布时间】:2013-01-05 21:48:45
【问题描述】:

我是一名 iOS 开发新手,希望能提供有关如何以编程方式在 UITableView 中创建多个 UISlider 和 UILabel 控件的任何帮助,如图所示:

如上面的样机图片所示,我只需要在更改相应的滑块(在同一行)时更新相关的标签文本。

使用下面的代码,我可以为表格视图中的每一行动态创建多个滑块控件,但无法在滑块的值更改时显示相应的标签并更新此标签文本。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ControlRowIdentifier = @"ControlRowIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ControlRowIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:ControlRowIdentifier];


        CGRect frame = CGRectMake(0.0, 0.0, 100.0, 10.0);
        UISlider *slider = [[UISlider alloc] initWithFrame:frame];
        [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
        [slider setBackgroundColor:[UIColor clearColor]];
        slider.minimumValue = 1;
        slider.maximumValue = 70;
        slider.continuous = YES;
        slider.value = 30;

        cell.accessoryView = slider;
    }
    NSUInteger row = [indexPath row];
    NSString *rowTitle = [list objectAtIndex:row];
    cell.textLabel.text = rowTitle;

    return cell;
}

//Does not work
- (IBAction)sliderAction:(id)sender {
    UISlider *sliderControl = (UISlider *)sender;
    int SliderValue = (int)roundf(sliderControl.value);
    UILabel *sliderLabel;
    sliderLabel.text = [NSString stringWithFormat:@"%d", SliderValue];
    [self.view addSubview:sliderLabel];
}

【问题讨论】:

  • 明确地说,您希望滑块值以每个单元格为基础将标签值更改为左侧?另外,请清楚什么是行不通的。
  • 请使用自定义单元格,这将对您有所帮助

标签: ios cocoa-touch uitableview uilabel uislider


【解决方案1】:

您应该将滑块创建移出 if 指令:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ControlRowIdentifier = @"ControlRowIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ControlRowIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
               reuseIdentifier:ControlRowIdentifier];
    }
    CGRect frame = CGRectMake(0.0, 0.0, 100.0, 10.0);
    UISlider *slider = [[UISlider alloc] initWithFrame:frame];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor]];
    slider.minimumValue = 1;
    slider.maximumValue = 70;
    slider.continuous = YES;
    slider.value = 30;

    cell.accessoryView = slider;

    NSUInteger row = [indexPath row];
    NSString *rowTitle = [list objectAtIndex:row];
    cell.textLabel.text = rowTitle;

    return cell;
}

【讨论】:

【解决方案2】:

我在这里看到了几个问题。

  1. 您永远不会实例化任何标签来显示滑块值。您需要对其进行实例化并将其放入表格单元格的视图层次结构中。
  2. 您将UITableViewDataSource 设置为滑块操作的目标。这使得很难知道哪个标签对应于哪个滑块。您应该自定义 UITableViewCell,或者通过子类化或将 UIView 子类添加到contentView。滑块应以单元格或视图子类为目标,并且标签更新可能发生在那里。还有其他方法可以做到这一点。
  3. 在您的sliderAction: 方法中,您永远不会初始化sliderLabel 变量。需要设置为cell对应的UILabel实例,如上。

【讨论】:

  • 非常感谢,卡尔。我会听从你的建议,会让你知道我的进展。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多