如何从子类UITableViewCell调用didSelectRowAtIndexPath

Hur*_*rkS 0 objective-c uitableview uiscrollview ios

我有一个UITableView,我用来显示数据,要求我有一个比iPhone screenWidth宽得多的单元格.

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

TableView.h

@property (strong, nonatomic) CustomCell *cell;
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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

CustomCell.h

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

- (void)drawCell;
Run Code Online (Sandbox Code Playgroud)

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];
}
Run Code Online (Sandbox Code Playgroud)

在上面的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];
}
Run Code Online (Sandbox Code Playgroud)

上面的方法绘制滚动视图,然后传递到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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

注意:我必须使用这些子类的原因是因为UIScrollView覆盖了UITableViewCell选择函数,所以我不得不将scrollview子类化为拦截触摸事件现在我希望我可以在你的帮助下调用原始tableview选择方法.

Sha*_*med 5

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

[yourTableView.delegate tableView:tableView didSelectRowAtIndexPath:aIndexPath];
Run Code Online (Sandbox Code Playgroud)

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

另一个解决方案是循环遍历单元格的超级视图,直到找到tableview.虽然这有其缺点,Apple有朝一日可能会改变视图层次结构的方式,这会破坏代码,就像他们从iOS6到iOS7一样,通过在一些视图周围包装另一层...

  • 如果您将单元格的tableView存储为单元格中的属性,就像对索引路径一样,那么您应该能够调用委托方法. (2认同)