2015-10-20 46 views
1

添加不同的自定義單元格我有多個自定義單元格。我將其中的兩個子類化。我想用故事板顯示具有不同標識符的不同單元格。到目前爲止,使用一個數組我能夠顯示一個子類的單元格,但是我的行數和顯示多個單元格的技術有問題。我使用延遲來不斷添加和更新我的表。如何TableView中

NSMutableArray *conv1; 
    NSString *l1; 
    NSString *l2; 
    NSString *l3; 
    NSMutableArray *allDialogue; 
    NSMutableArray *conv2; 
} 

@end 

@implementation PlotController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    conv1 = [[NSMutableArray alloc] init]; 
    conv2 = [[NSMutableArray alloc] init]; 
    allDialogue = [[NSMutableArray alloc] init]; 
    [allDialogue addObjectsFromArray:conv1]; 
    [allDialogue addObjectsFromArray:conv2]; 
    l1 = @"converstaion1"; 
    l2 = @"converstaion2"; 
    l3 = @"converstaion3"; 
    _tableV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
    [self performSelector:@selector(delay) withObject:nil afterDelay:2.0]; 
    [self performSelector:@selector(delay2) withObject:nil afterDelay:4.0]; 
} 

實現代碼如下配置

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MainStoryDialogue *dialogueCell = [tableView dequeueReusableCellWithIdentifier:@"PDialogue"]; 

    dialogueCell.textHere.text = [allDialogue objectAtIndex:indexPath.row]; 

    [self.tableV beginUpdates]; 
    [self.tableV endUpdates]; 

    return dialogueCell; 
} 

第二個問題是行數

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [allDialogue count]; 
} 
+0

加入多個定製的細胞就像我說 – Legenetic

+0

順便說一句,在開始和結束的更新呼叫您'cellForRowAtIndexPath'表視圖控制器生命週期方法什麼都不做。 – andrewbuilder

回答

1

我認爲,能在表視圖來顯示一個以上的自定義單元格,第一,在你的故事板中,你需要點擊你的tableView,在屬性檢查器下面你會看到一個名爲Prototype Cells的字段。選擇您想要使用的不同單元的數量,根據需要自定義它們,爲每個單元創建一個TableViewCell類,進行所有適當的連接,併爲每個單元設置一個唯一標識。已經這樣做了,在你的cellForRowAtIndexPath方法(無論基於何種條件下),你可以實例,你想爲使用您創建的自定義單元格類及相應的小區標識給定行特定的細胞。

因此,例如:

if (indexpath.row == 1) { 
    MainStoryDialogue *dialogueCell = [tableViewdequeueReusableCellWithIdentifier:@"PDialogue"]; 

//do all appropriate things if you have this cell 
} else { 
    CustomCell2 *customCell2 = [tableView dequeueReusableCellWithIdentifier:@"customCell2"]; 

//do all appropriate things if you had this cell 
} 
+0

感謝偉大的反饋,但它沒有工作,它的工作,直到行被更新的即時猜測其爲 - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{ 回報[allDialogue計數] }我可以 – Legenetic

+0

我加入到陣列的延遲更新只需更換這有什麼好 – Legenetic

+0

所以當你更新和添加一些你的陣列,調用[self.tableView reloadData];其中tableView是您的應用程序中對UITableView的引用。 –