您好我有一个UITableView,我dymanically插入细胞转化为它包含一个UIStepper和一个UILabel。 UILabel显示UIStepper的值。集的标签在由步进控制的细胞 - 目标C
- (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;
}
我已删除了部分上面的线是为了清晰,做的格式,但是这个作品,一切都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工作的方式 - 我只能发送发件人在这种情况下是步进到事件,这意味着它不能访问动态创建的标签。有谁知道我可以如何设置incrementStepper委托方法的标签文本?
好吧,我已经尝试过,它没有工作。我试过放行NSLog(@“%@”,[cell.contentView subviews]); NSLog(@“%d”,[[cell.contentView subviews] count]);在该方法的结尾,数组只包含cell.textlabel并且count是1.我不确定为什么会这样,因为它看起来应该是这样。 – Jamesla
尝试使用[cell.contentView addSubview:stepper];和[cell.contentView addSubview:label]; – Bonnie
很酷的工作,但是我不得不将subview instatiation行更改为UIView * subview = [[cell subviews] lastObject] ;.感谢您的帮助,我真的很感激。 – Jamesla