【问题标题】:Strange behavior UITableView using viewWithTag使用 viewWithTag 的奇怪行为 UITableView
【发布时间】:2018-06-30 03:15:00
【问题描述】:

我的表格视图中有一个 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

【问题讨论】:

  • 请显示完整的错误信息。并指出导致错误的确切代码行。
  • 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Mediprecios.RotatedView setText:]:无法识别的选择器发送到实例 0x7f8d9073f4f0”并且在 labelName.text = name 中的方法 cellForRowAtIndexPath 中崩溃;
  • 不处理标签,为什么不提供对FoldingCell 类的labelName 属性的只读访问权限,就像textLabelUITableViewCell 中提供的一样。
  • edit 你的问题与错误。细节属于问题,而不是 cmets。
  • 这是 Xcode 的一个错误,只是我将标签号从其他类似 1001 更改为完美。

标签: objective-c xcode uitableview


【解决方案1】:

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

尝试将标签更改为一个大的随机数,如 35432 并检查。

上述解决方案是消除错误的临时解决方法。良好的做法是通过子类化创建您自己的FoldingCell 实现并将标签的IBOutlet 连接到单元类并访问它。

【讨论】:

  • 哇,这是 Xcode 的错误!!!只有我将标签号更改为另一个大数字,例如 1001,1002,并且效果很好。谢谢你的建议。我也无法创建折叠单元的实现,因为我在 Swift 中使用了这个框架github.com/Ramotion/folding-cell
  • 不,这不是 Xcode 的错误,FoldingCell 中必须有一些带有标签 1 的视图。
  • 而且您始终可以继承 FoldingCell 类并创建自己的 Cell 类以更优雅的方式拥有此功能。
  • 不,因为我检查了所有视图和子视图并且没有标签 1,而且我运行应用程序所有显示的标签都很好(labelName.text)应用程序工作正常,但是当我点击在某个单元格中向下滚动,有时应用会崩溃
  • 对于这种特殊情况,我不能对 FoldingCell 进行子类化,因为它是在 SWIFT 中编程的,这是实现此功能的唯一方法。请查看此链接:github.com/Ramotion/folding-cell
猜你喜欢
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多