Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewCell内的事件在y值以下不工作_Iphone_Objective C_Ios_Cocoa Touch_Uitableview - Fatal编程技术网

Iphone UITableViewCell内的事件在y值以下不工作

Iphone UITableViewCell内的事件在y值以下不工作,iphone,objective-c,ios,cocoa-touch,uitableview,Iphone,Objective C,Ios,Cocoa Touch,Uitableview,我在使我的按钮在表格单元格中的某个y值以下工作时遇到一些问题。我正在使用一个名为“RowWhiskyContent”的自定义UITableViewCell类。默认高度为44px,低于该点,我的事件似乎不再触发。按钮显示得很好,低于该点的其他所有内容也显示得很好,但事件似乎不会触发。如果我把我的按钮放在中间(比如y=35),只有按钮的顶部触发事件,而底部没有做任何事情 以下是精简到esentials的代码: -(UITableViewCell *)tableView:(UITableView *)

我在使我的按钮在表格单元格中的某个y值以下工作时遇到一些问题。我正在使用一个名为“RowWhiskyContent”的自定义UITableViewCell类。默认高度为44px,低于该点,我的事件似乎不再触发。按钮显示得很好,低于该点的其他所有内容也显示得很好,但事件似乎不会触发。如果我把我的按钮放在中间(比如y=35),只有按钮的顶部触发事件,而底部没有做任何事情

以下是精简到esentials的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell  *cell = nil;

    if(![self createView:&cell])
    {
        UIImage *bottle = [UIImage imageNamed:@"icon_add.png"]; //image size: 22x22
        UIButton *bottleButton = [[UIButton alloc] initWithFrame:CGRectMake(60, 70, bottle.size.width, bottle.size.height)]; 

        [bottleButton setImage:bottle forState:UIControlStateNormal];
        [cell.contentView addSubview:bottleButton];
        [bottleButton addTarget:self action:@selector(addToCollection:) forControlEvents:UIControlEventTouchUpInside];

        cell.contentView.frame = CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, 160);
        //cell.frame = cell.contentView.frame; // Tried this, didn't work.
        //[tableView reloadData]; // Tried this too, didn't work either.
    }
    return cell;
}

// Check if cell exists and create the cell if it doesn't.
-(BOOL) createView: (UITableViewCell**) cell
{
    BOOL cellExists = YES;
    *cell = (RowWhiskyContent *) [myTableView dequeueReusableCellWithIdentifier:@"ContentIdentifier"];    
    if(*cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RowWhiskyContent" owner:self options:nil];
        *cell = [topLevelObjects objectAtIndex:0];
        cellExists = NO;
    }
    return cellExists;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 160;
}
因为我将单元格和contentView的高度都设置为160,所以我不确定这里出了什么问题。重新加载数据无效,设置cell.frame也无效

谁能告诉我我做错了什么? 提前谢谢

编辑: 添加了一个屏幕截图:


红色按钮工作正常,但如果我把它放在绿色按钮的位置,它就会停止工作。contentview的背景设置为紫色,因此可以解释紫色区域。单击单元格时,它会触发didSelectRowAtIndexPath,因此我猜单元格本身也足够大。

这肯定是一个内容大小问题。您的按钮将显示在框架外,类似于CSS中的溢出,但它们不会响应事件。因此,无论UIView包含什么UIButton,都需要确保其内容/框架/边界都设置得足够高。您还可以使用[cell.contentView sizeToFit]自动调整它以适应其内容


您绝对不应该在UITableView的协议方法中重新加载数据

听起来好像没有响应的控件被某种东西遮挡了,或者更可能的情况是,包含视图的框架没有正确设置,尽管正在绘制控件,但它位于父视图之外。由于某些单元格正在工作,我将首先查看表本身及其所在的任何容器视图,以查看表的一部分是否位于父视图框架之外。是否为表设置了两个代理?请尝试将tableView.clipsToBounds设置为“是”,以便查看实际活动视图的位置sits@rokjarc,不知道它应该做什么,但我看不出有什么不同@NeverBe,yes@Damo,我对你的评论不是100%的理解,但我做了一个情况截图(添加到问题中)。按钮似乎没有重叠。紫色区域是视图(背景颜色为紫色),当我单击紫色区域中的任何位置时,我会触发表格单元格的
didSelectRowAtIndexPath
,因此我猜我的表格行具有相同的高度。您可以尝试添加UIView调试套件。尝试激活它,然后单击非工作按钮。如果按钮上方有任何视图,DCIntrospect应该识别它(不管它是按单元格、tableview还是其他方式构造的)。由于未知原因,单元格的视图顺序如下:UITableViewCell->UIView->UIView。UITableViewCell.contentView正在使用上一个UIView,使其大于包含它的UIView。单元格的大小正确,contentView(上一个UIView)的大小正确,但中间视图的大小不正确。通过将UIView和contentView设置为相同的UIView来解决此问题。