2013-12-13 51 views
0

好吧,這個問題可能已被問了幾次,但我無法找到一個幫助我前進的例子。填充分段的UITableView

我試圖創建一個分段的UITableView作爲某種歷史行爲:它應該填充由HistoryBatchVO對象組成的NSMutableArray。這些HistoryBatchVO對象包含一個時間戳(NSDate)和一個名稱數組(NSMutableArray),後者又包含NameVO類型的對象,其中包含NSString的其他字符串。

我想使用時間戳作爲部分標題和來自NameVO的字符串以相應填充到表中的部分。

在我的桌子控制器我有:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [_dataModel.history count]; 
} 

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

(注:_dataModel.history是我HistoryBatchVOs的NSMutableArray中)。 ...但我想numberOfRowsInSection需要返回我的HistoryBatchVO.names數組中的對象數。問題是我該怎麼做?

此外,如何更改cellForRowAtIndexPath的執行情況以使其正常工作?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 
    HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.row]; 

    return cell; 
} 

UPDATE:解決了這個問題之後,這裏的工作代碼:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [_dataModel.history count]; 
} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    HistoryBatchVO *h = [_dataModel.history objectAtIndex:section]; 
    return [h.names count]; 
} 


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    HistoryBatchVO *h = [_dataModel.history objectAtIndex:section]; 
    return [NSString stringWithFormat:@"%@", h.timestamp]; 
} 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"Cell"; 
    UITableViewCell *cell = nil; 
    cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
    } 

    HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.section]; 
    NameVO *nameVO = [batchVO.names objectAtIndex:indexPath.row]; 
    cell.textLabel.text = nameVO.string; 

    return cell; 
} 
+0

的NSLog'_dataModel.description' –

回答

1

假設names是一個數組,數據類型的一些假設,程序骨架會像這樣的東西,但你必須適應你的需求。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [_dataModel.history count]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    HistoryBatchVO * h = [_dataModel.history objectAtIndex:section]; 
    return [h.names count]; 
} 

您也需要實現該部分的標題的事情,但我想你會喜歡格式化使用NSDateFormatter的日期。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    HistoryBatchVO * h = [_dataModel.history objectAtIndex:section]; 
    return h.date; 
} 

電池元件會是這樣的,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"Cell"; 
    UITableViewCell *cell = nil;  
    cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 
    if (cell == nil) 
     cell = [[UITableViewCell alloc] UITableViewCellStyle:UITableViewCellStylePlain reuseIdentifier:cellID]; 

    HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.section]; 
    NameVO * nm = [batchVO.names objectAtIndex:indexPath.row]; 
    cell.textLabel.text = nm.name 

    return cell; 
} 
+0

感謝您的幫助!我會盡量調整你的例子!然而numberOfRowsInSection現在會拋出一個錯誤:[__NSArrayM objectAtIndex:]:index_3超出範圍[0..2] – BadmintonCat

+0

如果您的numberOfSectionsInTableView返回[_dataModel.history count],那麼numberOfRowsInSection方法應該總是在[_dataModel.history objectAtIndex:部分]。這可能有問題。 – karim

+0

當我註銷我的_dataModel.history的計數時,它說4是正確的。在numberOfRowsInSection中記錄節號,它顯示3,0,1,2 ...因此它們不是按順序排列的。這可能與錯誤有關嗎? – BadmintonCat