English 中文(简体)
由于UISlider价值改变,没有更新价值的单元
原标题:Cell not updating value as UISlider value changed
  • 时间:2009-08-30 09:25:49
  •  标签:

我有一个问题正在引起我恐慌。 这完全是实时更新一架UITableViewCell,其价值为一架UISlider,位于另一颗UITableViewCell。

The problem

单元更新后 看来,牢房里的Label人的规模并不等于所代表的价值大小。 我将尽力作出更好的解释。

UISlider的最低月度为-100,最高值为100。 因此,表态第一次装满,cell.detailText.显示0.0%的巫师是ok,但当我搬动梯子,价值更大时,例如55.5%,标签不能保持这种额外性,显示“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

最佳回答

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 围绕这一问题开展工作,迫使标签在像此那样的扼杀开始时增加空间:

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

这样,我的梯子能够准确地将数字从0到999;因为你们的特性更简单地调整所期望的长度。

问题回答

为什么要使用<条码>保险单。 当你更新电池的值时? 我认为,如果你先把电梯的价值投入电池,即:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];

你们出入牢房的方式可能不会让你看到你所期望的囚室。 您最初分配的标签为1002? <代码>celda。 然后,仅仅检查,就将电池标签的价值定在不太模糊的地方。





相关问题
热门标签