2011-11-27 63 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

真的感謝)!! –