好吧,這個問題可能已被問了幾次,但我無法找到一個幫助我前進的例子。填充分段的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;
}
的NSLog'_dataModel.description' –