Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在由步进电机控制的单元格中设置标签-目标c_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Ios 在由步进电机控制的单元格中设置标签-目标c

Ios 在由步进电机控制的单元格中设置标签-目标c,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,嗨,我有一个UITableView,我正在动态地将包含UIStepper和UILabel的单元格插入其中。UILabel显示UIStepper的值 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifie

嗨,我有一个UITableView,我正在动态地将包含UIStepper和UILabel的单元格插入其中。UILabel显示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;
}
为了clarities的缘故,我删除了上面一些进行格式化的行,但是这样做很有效,一切正常

-(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委托方法设置标签的文本吗?

调用
[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子视图]
数组
,获得所需的标签,最好给标签一个
标记
值,并使用
查看带标记
调用
[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子视图]
数组
,获得所需的标签,最好为标签指定一个
标记
值,并使用
viewWithTag

您可以将标记设置为UIStepper作为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

}

您可以将标记设置为UIStepper为indexPath.row,并将标记设置为label为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

}

应使用步进器的值更新模型,然后基于模型中的该值设置标签的值。您应该在cellForRowAtIndexPath中为步进器提供一个与indexPath.row相等的标记,并在步进器的操作方法中,将模型中某个属性的值设置为步进器的值。然后,在相同的位置重新加载行

(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”将用于将stepper值存储为NSNumber。

您应使用stepper值更新模型,然后根据模型中的该值设置标签值。您应该在cellForRowAtIndexPath中为步进器提供一个与indexPath.row相等的标记,并在步进器的操作方法中,将模型中某个属性的值设置为步进器的值。然后,在相同的位置重新加载行

(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”将用于将stepper值存储为NSNumber。

如果有多个节,该怎么办?然后您可以这样设置标记:[label setTag:((indexPath.section&0xFFFF)并且在委托方法中可以这样获取行和节:NSInteger节=((sender.tag>>16)&0xFFFF);NSUInteger行=(sender.tag&0xFFFF);如果有多个节该怎么办?然后您可以这样设置标记:[label setTag:((indexPath.section&0xFFFF)并且在委托方法中可以这样获取行和节:NSUInteger节=((sender.tag>>16)&0xFFFF);NSUInteger行=(sender.tag&0xFFFF);好的,我试过了,但它不起作用。我试过将行NSLog(@“%@,[cell.contentView子视图]);NSLog(@“%d”,[[cell.contentView子视图]计数]);放在方法的末尾,数组只包含cell.textlabel,计数为1。我不确定为什么会出现这种情况,因为它似乎应该起作用。请尝试使用[cell.contentView addSubview:stepper];和[cell.contentView addSubview:label];很酷,但是我不得不将子视图安装行更改为UIView*subview=[[cell subview]lastObject];。感谢您的帮助,我非常感谢。好的,我已经尝试过了,但没有成功。我尝试过将行NSLog(@“%@”,[cell.contentView子视图];NSLog(@“%d”,[[cell.contentView子视图]计数]);在方法的末尾,数组仅包含cell.textlabel,计数为1。我不确定为什么会出现这种情况,因为它似乎可以工作。请尝试使用[cell.contentView addSubview:stepper];和[cell.contentView addSubview:label];很酷,但我不得不将子视图安装行更改为UIView*子视图=[[cell subview]lastObject];。感谢您的帮助,我非常感谢。