2011-11-27 79 views
4

我需要创建特定tableViewCell,这表明在第一触摸特殊子视图并隐藏它第二触摸该子视图包含一些标签或按钮。的UITableViewCell这表明在触摸子视图并隐藏它第二触摸

+0

和你的问题是?另外,到目前为止您尝试过哪些方面,您参考了哪些手册,以及为什么您的结果不能按照您需要的方式工作? – Till

+0

我是新的obj c,所以代码的结果是可怕的,我不明白怎么做是正确的,添加控件的视图或添加简单的控件,以及如何动画显示和子视图消失 –

+0

我不明白它是如何工作的,我使用addSubview将单元格添加到单元格,并将单元格高度50例如和按钮origin.y = 50,但是当我启动应用程序时,我可以看到此按钮而不是它隐藏在单元格内 –

回答

5

cellForRowAtIndexPath中,将tag属性添加到相关单元的子视图中。还要将子视图的hidden属性设置为YES。最后,将单元的selectionStyle设置为UITableViewCellSelectionStyleNone

if (thisIsTheIndexPathInQuestion) { 
    CGRect theFrame = CGRectMake(...); // figure out the geometry first 
    UIView *subview = [[UIView alloc] initWithFrame:theFrame]; 
    // further customize your subview 
    subview.tag = kSubViewTag; // define this elsewhere, any random integer will do 
    subview.hidden = YES; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    [cell.contentView addSubView:subview]; 
    [subview release]; 
} 

然后就反应到你在适当的UITableView委托方法描述一下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (thisIsTheIndexPathInQuestion) { // you know how to check this 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     UIView *subview = [cell viewWithTag:kSubViewTag]; 
     subview.hidden = !subview.hidden; // toggle if visible 
    } 
} 

确保你的“特殊”单元都有不同的CellIdentifier,这将正常工作。

+0

真的感谢)!! –