2014-07-16 52 views
-2

我有兩個TableViewCell在同一個TableViewController,我想知道如何設置它們兩個。具體地,在的cellForRowAtIndexPath如何在一個tableviewcontroller中設置多個tableviewcells?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SignInTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SignInTableViewCell" forIndexPath:indexPath]; 

    SignUpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SignUpTableViewCell" forIndexPath:indexPath]; 
    // Configure the cell... 

    return cell; 
} 

以下是在情節串連圖板(imgur.com/vCRGY6V)的圖像。我試圖設置這兩個單元格。我已經爲這兩個設置了TableViewCells,但沒有設置它們的TableViewController。 imgur.com/vCRGY6V

+0

咦?在同一索引路徑中不能有兩個不同的單元格。 (你也不能重新聲明這樣的變量。) –

+0

@JoshCaswell我知道,只是一個例子。我該怎麼做才能做到這一點? – user3398659

+1

對於初學者來說,更清楚地解釋你正在嘗試做什麼......也許有一張照片。 – nhgrif

回答

0

您只能從tableView:cellForRowAtIndexPath:返回一個單元格。您將需要使用一些邏輯來確定返回哪種類型的單元格。

例如:

if (indexPath.row == 0) { 
    // instantiate and return one type of cell 
} else { 
    // instantiate and return the other type of cell 
} 
-1
-(int) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 2; 
} 


-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellIdentifierSignUp = @"SignUpTableViewCell" 
    static NSString * cellIdentifierSignIn = @"SignInTableViewCell" 
    UITableViewCell * cell; 
     if (indexPath.row==0) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifierSignUp]; 
     } 
     else if (indexPath.row==1) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifierSignIn]; 
     } 
    return cell; 
} 
相關問題