ios - 具有多个自定义单元格滚动和重用问题的 uitableview

标签 ios objective-c xcode uitableview

我已经看到这个问题被问了很多,但还没有找到一个很好的例子来满足我的需要。人们到处指定第 0 行需要是这个单元格,第 1 行需要是那个单元格,而其他人则是其他东西。

我需要根据数据区分我的细胞。假设这就是我的 NSMutableArray 字典的样子:

{(
  actionContent = "some comment",
  actionType = "comment",
  datetime = "some date",
  whosAction = "some person"
),
(
  actionContent = "",
  actionType = "like",
  datetime = "some date", 
  whosAction = "another person"
),
(
  actionContent = "blah blah blah mentioned you",
  actionType = "mention",
  datetime = "some date",
  whosAction = "person"
)}

类似的东西,我知道语法可能有问题。为此,我创建了两个自定义单元格,并在 cellForRowAtIndexPath 中运行了一堆 if 语句来查看要使用哪个单元格。第一个单元格只有一个 UITextView,用于点赞,第二个单元格有两个 UITextView,用于评论和提及。

问题:当我滚动表格时,第二个单元格进入 View 并且我收到此错误(所有 UITextViews 都有点击手势):

-[FirstCell secondTap]: unrecognized selector sent to instance 0xa872020

FirstCell 没有“第二个”UITextView,因此不应该有任何 secondTap...所以显然我在 cellForRowAtIndexPath 中错误地使用了多个单元格。我现在将展示我的代码:

第一单元格:

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    UITextView *first = [[UITextView alloc]initWithFrame:CGRectMake(55, 15, 250, 20)];
    [first setFont:[UIFont systemFontOfSize:10]];
    [first setTextColor:[UIColor blueColor]];
    [first setEditable:NO];
    [first setScrollEnabled:NO];
    [first setSelectable:YES];
    [first setUserInteractionEnabled:YES];
    [first setAllowsEditingTextAttributes:YES];
    first.textContainerInset = UIEdgeInsetsMake(3, 0, 0, 0);
    UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc]init];
    _firstTap = firstTap;
    [first addGestureRecognizer:_firstTap];
    [self addSubview:_first];
}

第二单元格:

//same _first UITextView and a second one
    UITextView *first...

    UITextView *second = [[UITextView alloc]initWithFrame:CGRectMake(55, 20, 250, 30)];
    [second setFont:[UIFont systemFontOfSize:10]];
    [second setEditable:NO];
    [second setScrollEnabled:NO];
    [second setSelectable:YES];
    [second setUserInteractionEnabled:YES];
    [second setAllowsEditingTextAttributes:YES];
    second.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc]init];
    _secondTap = secondTap;
    [second addGestureRecognizer:_secondTap];
    _second = second;
    [self addSubview:_second];

我确定问题出在此处...cellForRowAtIndexPath:

static NSString *cellIdentifier = @"cell";
if (myData.count != 0) {
    if ([[[myData objectAtIndex:indexPath.section]valueForKey:@"actionContent"]isEqualToString:@""]) {
        FirstCell *cell = (FirstCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[FirstCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        //run if statements to checks if this is a likes or friend requests
        return cell;
    }else{
        SecondCell *cell = (SecondCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell){
            cell = [[SecondCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        //run if statements to check if comments or mentions
        return cell;
    }
}else{
    //use FirstCell to display empty message
}
return nil;

我希望这不会太长,我希望我说的很清楚。我想我初始化它们是错误的。关于如何使这项工作有任何建议吗?

顺便说一句:我用了 this example但正如我所说,他们已经设置好行,他们知道哪一行应该是哪个单元格。我需要经常检查。

最佳答案

您为两种不同的细胞类型使用相同的细胞标识符(“细胞”)。尝试为您的 FirstCell 提供类似 @"firstCell" 的标识符,为您的第二个提供类似 @"secondCell" 的标识符。

关于ios - 具有多个自定义单元格滚动和重用问题的 uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23883802/

相关文章:

objective-c - 在 iOS 应用程序中获取和显示来自 Facebook 的图像

ios - 如何设置@IBAction 来定位 UIView 子类

ios - 在 iOS 中根据其中的 View Controller 动态调整容器 View 的大小

ios - 在 swift (IOS) 中以编程方式导航

objective-c - cocoa 应用;在预览中打开文件 (pdf)

ios - 多个 UIWindows 以及哪个控制状态栏方向

objective-c - IOS Dev 错误处理最佳实践

ios - 关闭应用程序后,Safari IOS 不会保留使用 JavaScript 设置的 Cookie

ios - 添加表格 View 单元格遵循 1 个自定义样式?

ios - 上传IPA,奇怪的问题: ERROR ITMS-90032 :“Invalid Image Path - No image found at the path referenced under key ' $key': '$path'