【问题标题】:How to call didSelectRowAtIndexPath from subclassed UITableViewCell如何从子类 UITableViewCell 调用 didSelectRowAtIndexPath
【发布时间】:2014-05-02 14:18:36
【问题描述】:

我有一个 UITableView,我用它来显示需要我拥有比 iPhone 屏幕宽度更宽的单元格的数据。

因此,我创建了一个包含子类 UIScrollView 的子类 UITableViewCell。这就是我的代码的样子。

TableView.h

@property (strong, nonatomic) CustomCell *cell;

TableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cellDictionary = [xmlMArray objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell.
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    cell.itemsDictionary = cellDictionary;
    cell.currentIndex = indexPath;

    cellDictionary = nil;
    [cell drawCell];

    return cell;
}

在上述方法中,您可以看到我正在调用我创建的自定义 UITableViewCell。

CustomCell.h

@property (strong, nonatomic) NSDictionary *itemsDictionary;
@property (strong, nonatomic) MyScrollView *scrollCell;
@property (strong, nonatomic) NSIndexPath *currentIndex;

- (void)drawCell;

CustomCell.m

- (void)drawCell
{
    scrollCell = [[MyScrollView alloc] init];
    scrollCell.itemsDictionary = itemsDictionary;
    [scrollCell drawCell];
    scrollCell.backgroundColor = [UIColor whiteColor];
    scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);

    [[self contentView] addSubview:scrollCell];
}

在上面的drawCell方法中我调用了一个自定义的scrollView

MyScrollView.m

- (void)drawCell
{
    bgGray = NO;
    fNameString = [[UILabel alloc] init];
    fNameString.backgroundColor = [UIColor clearColor];
    fNameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
    fNameString.text = [itemsDictionary objectForKey:@"fName"];

    lNameString = [[UILabel alloc] init];
    lNameString.backgroundColor = [UIColor clearColor];
    lNameString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
    lNameString.text = [itemsDictionary objectForKey:@"lName"];

    addressString = [[UILabel alloc] init];
    addressString.backgroundColor = [UIColor clearColor];
    addressString.frame = CGRectMake(220.0, 10.5, addressString.frame.size.width, 50.0);
    addressString.text = [NSString stringWithFormat:@"Address: %@: %@",[itemsDictionary objectForKey:@"aNumber"] ,[itemsDictionary objectForKey:@"aString"]];
    [addressString sizeToFit];

    [self setContentSize:(CGSizeMake((220.0 + addressString.frame.size.width)+15, 45.0))];

    [self addSubview:fNameString];
    [self addSubview:lNameString];
    [self addSubview:addressString];
}

上面的方法绘制滚动视图,然后传递到CustomCell,一切正常并正确显示,下面是我用来检测cusome滚动视图上的触摸的方法,它与上面的方法在同一个类中,看起来像这样。

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
    NSLog(@"touch scroll");
    // If not dragging, send event to next responder
    if (!self.dragging) {
        // touch detected...
        //How can I now call didSelectRow from TableView?
    } else {
        [super touchesEnded: touches withEvent: event];
    }
}

所以我的问题是,如何使用这个 touchEnded 方法来调用原始 TableView 类中的 didSelectRowFromIndexPath 方法?或者有没有比我目前正在做的更好的方法?

注意:我不得不使用这些子类的原因是因为 UIScrollView 覆盖了 UITableViewCell 选择功能,所以我必须子类化滚动视图来拦截触摸事件现在我希望我可以调用原始的 tableview 选择方法在你的帮助下。

【问题讨论】:

    标签: ios objective-c uitableview uiscrollview


    【解决方案1】:

    如何使用以下代码调用委托函数:

    [yourTableView.delegate tableView:tableView didSelectRowAtIndexPath:aIndexPath];
    

    在您的子类单元格中存储对 tableview 和 indexPath 的弱引用。

    另一种解决方案是遍历单元格的超级视图,直到找到表格视图。不过,这有其缺点,Apple 可能有一天会改变视图层次结构的方式,这将破坏代码,就像他们从 iOS6 到 iOS7 所做的那样,通过在一些视图周围包裹另一层......

    【讨论】:

    • 我将每个单元格的 indexpath 传递给它自己... cell.currentIndex = indexPath; 所以如果我能以某种方式将当前 tableView 传递给子类单元格和滚动视图可能我可以这样调用 didselectrowatindexpath 吗?
    • 如果您将单元格的 tableView 作为属性存储在单元格中,就像您为索引路径所做的那样,那么您应该能够调用委托方法。
    • 正如@Paulw11 所说,将其存储在具有弱引用的属性中以避免循环依赖,或者另一种方法是查找单元格的超级视图,请参阅answer 代码 sn-p
    • @Shams。如果您可以编辑您的问题以添加此问题,我可以删除我的反对票,该票是基于您的原始解决方案而投的,该解决方案可能会崩溃
    • 是的,它成功了!通过将引用传递给 tableview 一直到子类滚动视图,我可以调用 didSelectRowAtIndexPath... 感觉有点像 hack 的方式,但它是迄今为止我发现的最好的方式!谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多