2013-08-21 41 views
-1

我試過搜索,但目前我似乎無法找到答案。如何動態組合原型細胞目標-c

目前,我使用原型單元格動態填充我的數據。

我需要根據日期動態分組單元格。

讓我們說在1/1/2001,我有3行。在2/1/2001,我有5行。我似乎無法找到展示如何動態分組細胞的指南或樣本。以下是我的部分代碼。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
// Return the number of sections. 
return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
// Return the number of rows in the section. 
return [someArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"cell"; 
IssuesViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[IssuesViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier]; 
} 

// Configure the cell... 
currentSomething = [[Something alloc] init]; 
currentSomething = [somethingDiscovered objectAtIndex:indexPath.row]; 

cell.typeLabel.text = currentSomething.type; 
cell.titleLabel.text = currentSomething.title; 
cell.begDateLabel.text = currentSomething.begDate; 

return cell; 
} 

更新於23/8/2013:

現在我可以分組它動態,但是,我有在細胞顯示正確的數據的問題。對象A,B,C,D,E,F,G,H,I,J,K。我有4個部分。假設顯示A,B,C,D,E,F,G。[3],假設顯示H,I,J,K。

但是,現在只顯示A ,B,C,D,E,F,G部分[0]。 A,B,C,D,在[3]節中。請協助。

解決方案:

這樣的
someIssue = [[someIssues objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
+0

所以,你找不到任何關於分組'UITableView'什麼? – Desdenova

+0

我發現的大多數解決方案,其部分都是靜態的。 –

回答

2

的一種方式,是有一個結構來填充你的tableview,每當它得到修改只是重新加載所有的tableview或只是其中的一部分。

– reloadData //Reload all of your table view's data 
– reloadRowsAtIndexPaths:withRowAnimation: //Reload some rows 
– reloadSections:withRowAnimation: //Reload some defined sections 

更多關於如何使用這些方法上可以找到UITableView Class Reference

你的結構可以是這樣簡單:

//This will represent two sections with two cells each 
`NSArray *contentArray = @[@[@"Cell A",@"Cell B"],@[@"Cell C",@"Cell D"]];` 

即包含將表示元素的數組部分,每個元素/部分是一個包含所有單元格值的數組。

並在您的TableView的委託和數據源的方法,你應該這樣做

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (contentArray) { 
     return contentArray.count; 
    } 
    return 0; 
} 

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

適用於我的分組!但是現在我遇到了顯示數據的問題。將在幾分鐘內更新此頁面。 –

+0

更新了我的問題頁面。 –

+0

我只需要編輯它someIssue = [[someIssues objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; ,這樣它會顯示我想要的數據。 –