2015-04-07 32 views
0

我有一個具有兩個不同自定義表格單元格的UITableView。我啓動應用程序後,第一個單元格顯示正常。第二個單元格將在您點擊它們時出現。可摺疊單元格

任何人都可以幫助我或有想法嗎?

非常感謝。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *MyIdentifier = @"customCell2"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if(cell == nil){ 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

     cell.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.textColor = [UIColor grayColor]; 
     cell.textLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:9.0]; 
    } 
    return cell; 

} 
+1

你怎麼能點擊它後,如果還沒有出現一個細胞出現?什麼是用戶點擊使一個單元格出現? – user2320861

回答

1

在過去完成自定義UITableViewCell之後,我通常會在自定義類本身中處理n​​ib加載。

自定義單元格的基本標題。

@interface RequestsTableViewCell : UITableViewCell { 
    // Ivars. 
} 
// Properties. 
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType; 
// Other methods, etc. 
@end 

具有指定初始值設定項的自定義單元格。

@implementation RequestsTableViewCell 

- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"RequestsTableViewCell" owner:self options:nil]; 
     self = [nibArray objectAtIndex:0]; 
     requestModel = model; 
     queryType = requestType; 
     [self setRequestThumbnail]; 
     [self setRequestCategory]; 
     [self setRequestAddress]; 
     [self setRequestStatusDate]; 
     [self setRequestStatus]; 
     [self setRequestFollowed]; 
     [self setRequestComment]; 
     [self setAppearance]; 
    } 

    return self; 
} 

有也將是自定義的UITableViewCell對應,並在身份檢查器中設置的自定義類的自定義廈門國際銀行。

在UITableViewController中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellId = @"Cell Id"; 
    RequestModel *request = nil; 

    // Other code for search, etc 

    request = [self.serviceRequests objectAtIndex:indexPath.row]; 

    RequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 

    if(!cell) { 
     cell = [[RequestsTableViewCell alloc] initWithRequestModel:request style:UITableViewCellStyleDefault reuseIdentifier:cellId forQueryType:queryTypeIndicator]; 
    } 

    return cell; 
} 

這聽起來像你有多個自定義單元格類型在你的問題?你能詳細說明它是如何運作的嗎?你說你必須點擊一個單元格才能出現另一個單元格,你能解釋一下這種交互嗎?

1

我做了類似的事情,但是讓單元格「展開」,而不是添加新的單元格。當然,你沒有兩個單元格,但你可以調整你的一個單元格,添加子幀,...

你可以在你的UITableViewCell對象(BOOL cellIsExpanded)中保留一個布爾值,並在輕擊手勢時設置它。然後在TableViewCell的drawRect中,相應地佈局你的單元格。

實施例的代碼,就擴大,使細胞高度20 - > 80,並添加一個UIButton:

在TableViewController,過載heightForRowAtIndexPath(這將調整你細胞如果 '擴展'):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    YourEntity *record = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    if (!record.cellIsExpanded) 
     return 20.; // cell is smaller if collapsed 
    else 
     return 80.; // bigger cell 
} 

在TableViewCell,添加或刪除子:

@interface MyTableViewCell() 
@property(nonatomic) BOOL cellIsExpanded 
@property(strong, nonatomic) UITextField *myTextField; 
@property(strong, nonatomic) UIButton *clickMeButton; 
@end 

@implementation MyTableViewCell 

- (void)drawRect:(CGRect)rect { 
    if(!self.cellIsExpanded){ 
     // layout your collapsed cell, for example: 
     self.myTextField = [[UITextField alloc] initWithFrame:self.frame]; 
     self.myTextField.text = @"Collapsed cell"; 
     // remove button, only present in expanded view : 
     self.clickMeButton=nil; 
    } 
    else{ 
     self.myTextField.text = @"Expanded cell"; 
     // add button below textfield 
     self.clickMeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 10, 10)]; 
    } 
} 
@end 
+0

謝謝大家的解決方案。我的解決方案是,我擴大表格單元:) – NoBody

+0

但現在我有另一個問題。單元格的內容現在重疊在另一個單元格上。 你如何解決這個問題?內容稍後繪製? – NoBody

+0

你的細胞是否足夠高?你使用heightForRowAtIndexPath嗎?你不能增加展開的單元格的高度嗎? – Wouter