2013-05-22 23 views
0

您好我有一个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委托方法的标签文本?

回答

3

呼叫[sender superview]得到,它在细胞内,

-(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 subviews]array,并获得您需要的标签,最好给标签中的tag值,并使用viewWithTag

+0

好吧,我已经尝试过,它没有工作。我试过放行NSLog(@“%@”,[cell.contentView subviews]); NSLog(@“%d”,[[cell.contentView subviews] count]);在该方法的结尾,数组只包含cell.textlabel并且count是1.我不确定为什么会这样,因为它看起来应该是这样。 – Jamesla

+0

尝试使用[cell.contentView addSubview:stepper];和[cell.contentView addSubview:label]; – Bonnie

+1

很酷的工作,但是我不得不将subview instatiation行更改为UIView * subview = [[cell subviews] lastObject] ;.感谢您的帮助,我真的很感激。 – Jamesla

0

您可以将标签设置为indexPath.row并将标签设置为indexPath.row + 999。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIStepper *stepper = [[UIStepper alloc]init]; 
    stepper.tag = indexPath.row; 

    UILabel *label = [[UILabel alloc]init]; 
    label.tag = indexPAth.row + 999; 

    //...... 
} 

现在UIStepper的委托方法,你可以找到的标签在该单元格这样

-(void)incrementSkillStepper:(id)sender 
{ 
    UIStepper *stepper = (UIStepper*)sender; 
    UILabel *label = (UILabel *)[self.view viewWithTag:sender.tag + 999]; 
    //now this is same label as you want. Now you can change the value of label as you want 

} 
+0

如果有多个部分,该怎么办? – Bonnie

+0

然后你可以像这样设置标签: [label setTag :((indexPath.section&0xFFFF)<< 16)| (indexPath.row&0xFFFF)]; –

+0

而在委托方法中,您可以像这样获取行和部分: NSUInteger section =((sender.tag >> 16)&0xFFFF); NSUInteger row =(发件人。标记&0xFFFF); –

0

您应该更新与步进值的模型,然后将标签的价值基础上设置该模型中的价值。你应该给步进器的cellForRowAtIndexPath中的一个标签,它等于indexPath.row,并且在步进器的操作方法中,将模型中某个属性的值设置为步进器的值。然后,重新加载该行在同一个indexPath。

(void)incrementSkillStepper:(UIStepper *)sender { 
    NSInteger row = sender.tag; 
    [self.theData[row] setObject:@(sender.value) forKey:@"stepperValue"]; 
    [self.tableView reloadRowsAtIndexPaths: @[row] withRowAnimation:UITableViewRowAnimationNone]; 
} 

在的cellForRowAtIndexPath,你有这样的事情来填充标签:

UILabel *label = [[UILabel alloc]init]; 
    label.text = [NSString stringWithFormat:@"%@", self.theData[indexPath.row][@"stepperValue"]]; 

在这个例子中,我假设你已经字典数组(海图)具有所有数据你需要填充单元格。其中一个字典键“stepperValue”将用于将步进值存储为NSNumber。