2011-08-24 54 views
1

我正在創建一個包含兩個部分的uitableview,部分== 0有5行,部分== 1有1行。如何定義uitableviewcells

我也已經在一個objective-c類中聲明瞭幾個函數,我想把它們分別插入到五行中。但我不知道如何實現這一點。

我想這是像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

//---------- In here put a bunch of IF statments, declaring each cell? and linking it to 
//---------- the function I have declared in the objective-c class 

    return cell; 
} 

回答

1

通常情況下,單元格設置爲tableView:cellForRowAtIndexPath:。確定單元格點擊時調用的函數通常在tableView:didSelectRowAtIndexPath中完成。

tableView:cellForRowAtIndexPath:設置單元格可以指定部分,並使用indexPath這樣排:

if(indexPath.section == 0){ 

    if(indexPath.row == 0){ 

     // setup this cell... 

    }else if(indexPath.row == 1){ 

    }// ... 

}else if(indexPath.section == 1){ 

    if(indexPath.row == 0){ 

    }else if(indexPath.row == 1){ 

    }// ... 
} 

確定哪些功能,當用戶點擊每個小區被調用時做類似的事情在tableView:didSelectRowAtIndexPath。 Apple的相關文檔可以在here找到。

+1

嘗試使用'enum',它有助於可讀性。這樣,如果單元格是靜態的,你可以像'indexPath.row = MyTableViewRowFruit'或'indexPath.row = MyTableViewSectionResults' – Can

0

通常這個功能將拉動數據從一個陣列,數據庫來配置電池,或其他一些索引集合:

if ([indexPath section] == 0) { 
    [[cell textLabel] setText:[sectionOneValues objectAtIndex:[indexPath row]]]; 
} 
else if ([indexPath section] == 0) { 
    [[cell textLabel] setText:[sectionTwoValues objectAtIndex:[indexPath row]]]; 
} 

在你的情況聽起來像你想要「鉤入」0節中5行的其他函數,所以你應該做的不是在if子句中使用sectionOnValues數組。如果不知道你的意思是用這些函數來做什麼,有點難。

0

取決於,所有的單元格是否相等,但只是改變它們的內容?那樣的話,你會是對的。另一方面,如果您打算在單元格中添加更多的標籤,圖像或其他內容,那麼對於每個不同的行,您將不得不調整單元格的創建方式,因此,必須添加新的CellIdentifiers以避免混合單元格,並更改if (cell == nil) {}中的代碼以配置每個單元設計。

+0

每個單元格將搜索一個數據庫查詢請求。但是每個單元格都會打開一個新的tableview,其中有一些選項可供選擇..我只是想弄清楚如何做到這一點..我想我已經把它放在腦海中了,但今天坐在我的電腦前,我的思想是隻是空白。 –

+0

來自Apple的示例代碼通常在開始時非常有用。 – SundayMonday

相關問題