在由步进器控制的单元格中设置标签-物镜c [英] Set a label in a cell that is controlled by a stepper - objective c
问题描述
我有一个UITableView,并且正在动态地向其中插入包含UIStepper和UILabel的单元格。 UILabel显示UIStepper的值。
Hi I have a UITableView and I am dymanically inserting cells into it that contain a UIStepper and a UILabel. The UILabel shows the value of the UIStepper.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
[cell.textLabel setText:[self.myarray objectAtIndex:indexPath.row]];
UIStepper *stepper = [[UIStepper alloc]init];
UILabel *label = [[UILabel alloc]init];
label.text = [NSString stringWithFormat:@"%.f", stepper.value];
[cell addSubview:stepper];
[cell addSubview:label];
[stepper addTarget:self action:@selector(incrementStepper:) forControlEvents:UIControlEventValueChanged];
return cell;
}
为了清晰起见,我删除了上面的某些行进行格式化,但是这可行,一切都很好。
I have removed some of the above lines that do the formatting for clarities sake but this works and everything is ok.
-(void)incrementSkillStepper:(id)sender
{
UIStepper *stepper = (UIStepper*)sender;
//set the label that is in the same cell as the stepper with index stepper.value.
}
当我单击特定单元格中的步进器时,我希望标签位于同一位置单元格增加,但是我的问题是addtarget的工作方式-我只能发送发送者,在这种情况下是事件的步进器,这意味着它无法访问动态创建的标签。有人知道我怎么可以通过增量步进代理方法设置标签的文本吗?
When I click the stepper in the specific cell I want the label in the same cell to increment, but my problem is in which the way that addtarget works - I can only send the sender which in this case is the stepper to the event which means it doesnt have access to the dynamically created label. Does anybody know how I can set the text of the label from the incrementStepper delegate method?
推荐答案
调用 [发件人超级视图]
来获取它所在的单元格,
Call [sender superview]
to get the cell that it's in,
-(void)incrementSkillStepper:(id)sender
{
UIStepper *stepper = (UIStepper*)sender;
UITableViewCell* cell = [stepper superview];
UIView* subview = [[cell subviews] lastObject]; // Make sure your label *is* the last object you added.
if ([subview isKindOfClass:[UILabel class]]) {
// do what you want
}
}
您还可以遍历 [cell.contentView子视图]
array
,并获取所需标签,最好为标签提供 tag
值,并使用 viewWithTag
you can also loop thru the [cell.contentView subviews]
array
, and get the label you need, better give the label a tag
value, and use viewWithTag
这篇关于在由步进器控制的单元格中设置标签-物镜c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!