1

我想获取自定义创建的 uitableviewcell 的父视图。我尝试使用 self superview 来获取父 tableview ,但它没有用。

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization cod
        UITableView *tableView = (UITableView *)[self superview];
        NSIndexPath *indexPath = [tableView indexPathForCell:self];
        NSLog(@"%d %d", indexPath.section, indexPath.row);
...
}

Nslog 输出的值完全为 0。

@jrturton 给出了 self superview 不能引用其父视图的原因。但下一个问题是我想在单元格初始时获取 indexPath。

4

3 回答 3

1
- (void)didTapButton:(id)sender {
    // Cast Sender to UIButton
    UIButton *button = (UIButton *)sender;

    // Find Point in Superview
    CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];

    // Infer Index Path
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];

    // Fetch Item
    NSDictionary *item = [self.dataSource objectAtIndex:indexPath.row];

    // Log to Console
    NSLog(@"%@", item[@"title"]);
}
于 2014-04-09T07:14:51.847 回答
0

您必须遍历视图层次结构。 将帮助你

于 2012-05-14T10:10:08.830 回答
-3

尝试使用[cell.contentView addSubview:imgTemp];希望它会帮助你。

于 2012-05-14T10:34:39.980 回答