当 UISlider 值发生更改时,单元格未更新值
我有一个问题让我发疯。这一切都是关于使用另一个 UITableViewCell 内的 UISlider 的值实时更新 UITableViewCell。
问题
单元格已更新,但单元格中标签的大小似乎与所表示值的大小不适应。我会尽力解释得更好。
UISlider 的最小值为 -100,最大值为 100。因此,第一次加载 TableView 时,cell.detailText.text 显示 0.0% 女巫显示正常,但是当我移动时滑块和值更大,例如 55.5%,标签无法容纳这些额外的字符,它显示“55.....”
这就是我创建保存 UISlider 值的单元格的方法 在 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 方法中创建 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)”
提前感谢您的帮助
哈维
I have a question that is driving me crazy. It is all about updating, in real time, an UITableViewCell with the value of an UISlider that is inside another UITableViewCell.
The problem
The cell is updated but It seems that the size of the Label in the cell does not addapt to the size of the value represented. I will try to explain better.
The UISlider has a minimumValue of -100 and a maximum value of 100. So, the first time the TableView is loaded, the cell.detailText.text shows 0.0% witch is displayed ok, but when I move the slider and the value is bigger, for example 55.5%, the label can not hold this extra characters and it displays "55....."
This is how I create the cell that holds de UISlider Value create the UISlider in the cellForRowAtIndexPath delegate method
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProductCellIdentifier] autorelease];
cell.textLabel.text=@"Performance";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];
cell.tag=1002;
This is how I create de UISlider, in the cellForRowAtIndexPath method:
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];
And this is what I do in the sliderChanged method in order to update the cell.
UITableViewCell *celda= (UITableViewCell *)[[self tableView] viewWithTag:1002];
celda.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];
I suspect that the solution is about the use of the reloadData method of the UITableView but not sure. I have tried inserting a self.tableView.reloadData
in the sliderChanged method but I got then the UISlider does not move and finally I got this exception:
Terminating app due to uncaught exception 'NSRangeException', reason: ' -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
Thanks in advance for your help
Javi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您更新单元格的值时,为什么要使用
currencyStyleStringFromNumber:
?我认为使用与第一次将滑块的值放入单元格时使用的相同类型的初始化程序会得到更好的结果,即:Why are you using
currencyStyleStringFromNumber:
when you update the cell's value? I would think you'd get better results using the same kind of initializer you do when you first put the slider's value into the cell, that is:您访问单元格的方式(通过标签)可能无法获得您期望的单元格。你最初不是分配了标签1002吗?检查
celda
的值。然后,为了检查,将单元格标签的值设置为不太模糊的值。The way you access the cell (by tag) might not get you the cell you expect. Didn't you originally assign the tag 1002? Check the value of
celda
. And then, just to check, set the value of the cell's label to something less obscure.问题是,仅当要在单元格中绘制标签时,detailTextLabel 宽度是固定的并进行计算。
因此,当您使用“0.0%”初始化标签时,正好有 4 个字符的空间,并且您无法正确可视化超过该字符的任何内容;额外的字符要么用“...”修剪,要么标签不显示任何内容。
我通过强制标签在字符串开头包含额外的空格来解决这个问题,如下所示:
通过这种方式,我的滑块可以正确地可视化从 0 到 999 的数字;因为你有更多的字符,只需调整所需的长度。
the problem is that detailTextLabel width is fixed and computed only when the label is going to be drawn in the cell.
so, when you initialize the label with "0.0%" there is room for exactly 4 characters, and you cannot visualize correctly anything with more than that; extra charactes are either trimmed with "..." or the label just doesn't display anything.
i worked around this issue by forcing the label to contain additional spaces at the beginning of the string like this:
in this way, my slider can visualize correctly numbers from 0 to 999 correctly; since you have more characters just adjust the length desired.