iphone – 当UISlider值改变时,Cell不更新值

我有一个让我发疯的问题.它是关于实时更新UITableViewCell,其中UISlider的值位于另一个UITableViewCell中.

问题

单元格已更新但似乎单元格中Label的大小不会增加所表示值的大小.我会尝试更好地解释.

UISlider的minimumValue为-100,最大值为100.因此,第一次加载TableView时,cell.detailText.text显示0.0%的女巫显示正常,但是当我移动滑块并且值更大时,例如55.5%,标签不能容纳这个额外的字符,它显示“55 ……”

这就是我创建包含de UISlider Value的单元格在cellForRowAtIndexPath委托方法中创建UISlider的方法

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProductCellIdentifier] autorelease];
cell.textLabel.text=@"Performance";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];
cell.tag=1002;

这是我在cellForRowAtIndexPath方法中创建de UISlider的方法:

cell = [[[UITableViewCellalloc] initWithFrame:CGRectZero                                                           reuseIdentifier:ProductCellIdentifier] autorelease];

slider = [[UISlideralloc] initWithFrame:CGRectMake(90, 12, 200, 25)];

slider.maximumValue = 100;
slider.minimumValue = -100;
slider.continuous = TRUE;

[slideraddTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];

[cell.contentView addSubview:slider];
cell.tag=0;
[slider release];  

这就是我在sliderChanged方法中所做的,以便更新单元格.

UITableViewCell *celda= (UITableViewCell *)[[self tableView] viewWithTag:1002];
celda.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];

我怀疑解决方案是关于使用UITableView的reloadData方法但不确定.我试过在sliderChanged方法中插入一个self.tableView.reloadData但是我得到了UISlider没有移动,最后我得到了这个异常:
因未捕获的异常’NSRangeException’而终止应用程序,原因:’ – [NSCFArray objectAtIndex:]:索引(0)超出边界(0)’

在此先感谢您的帮助
哈维

最佳答案 问题是detailTextLabel宽度是固定的,只有在要在单元格中绘制标签时才计算.

因此,当您使用“0.0%”初始化标签时,恰好有4个字符的空间,并且您无法正确显示任何超过该标签的内容;额外的字符要么用“……”修剪,要么标签不显示任何东西.

我解决了这个问题,强制标签在字符串的开头包含额外的空格,如下所示:

while ([string length] <= 4)
      string = [NSString stringWithFormat:@" %@",prestring];
cell.detailTextLabel.text = string;

通过这种方式,我的滑块可以正确显示0到999之间正确的数字;因为你有更多的字符只需调整所需的长度.

点赞