使用viewWithTag

时间:2018-06-30 03:15:01

标签: objective-c xcode uitableview

我在表视图中有一个UILabel,并将标记手动设置为1

我有此代码,当我运行该应用程序时,它可以很好地工作。我确实向下滚动并可以正常工作,名称已正确填写。

但是当我单击该单元格并再次向下滚动时,有时该单元格中的标签有如下错误:

无法识别的选择器已发送到实例0x7fc01d74a710 <-这是我的标签,并且我打印了错误说明:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[[Mediprecios.RotatedView setText:]:无法识别的选择器发送到实例0x7f8d9073f4f0'   并在labelName.text = name中的cellForRowAtIndexPath方法中崩溃;

po 0x7fc01d74b370
<Mediprecios.RotatedView: 0x7fc01d74b370; frame = (0 100; 345 100); alpha = 0; tag = 1; layer = <CALayer: 0x600000827dc0>>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FoldingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

   UILabel *labelName = [cell viewWithTag:1];
   NSString *name = [displayItems objectForKey:@"Name"];
   labelName.text = name;

   return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   FoldingCell *cell = [tableView cellForRowAtIndexPath:indexPath];

   if (cell.isAnimating) {
       return;
   }

   double duration = 0;
   NSNumber *height = [self.cellHeights objectAtIndex:indexPath.row];
   bool cellIsCollapsed = height.floatValue == self.kCloseCellHeight;
   if (cellIsCollapsed) {
        [self.cellHeights setObject:[NSNumber numberWithFloat:self.kOpenCellHeight] atIndexedSubscript:indexPath.row];

    [cell unfold:YES animated:YES completion:nil];
    duration = 0.5;

} else {
    [self.cellHeights setObject:[NSNumber numberWithFloat:self.kCloseCellHeight] atIndexedSubscript:indexPath.row];
    //[cell selectedAnimation:false animated:true completion: nil];
    [cell unfold:NO animated:YES completion:nil];
    duration = 0.8;
}

[UIView animateWithDuration:duration delay:0 options:0 animations:^{
    [tableView beginUpdates];
    [

tableView endUpdates];
    } completion:nil];

}

PD:我使用的是这个库https://github.com/Ramotion/folding-cell

1 个答案:

答案 0 :(得分:1)

查看错误[Mediprecios.RotatedView setText:],您的代码实际上是在尝试在视图而不是在标签上调用setText方法。
UILabel *labelName = [cell viewWithTag:1];我的猜测是您的牢房中有任何{1}标签为1的UIView,而不是UILabel
因此,labelName.text = name;这行实际上尝试在UIView上设置文本,因为labelNameUIView

尝试将标签更改为较大的随机数,例如35432,然后检查。

以上解决方案是解决该错误的临时解决方法。优良作法是通过子类化FoldingCell来创建自己的实现,并将标签的IBOutlet连接到单元格类并进行访问。

相关问题