【发布时间】:2013-12-13 08:20:07
【问题描述】:
好的,这个问题可能已经被问过几次了,但我找不到一个可以帮助我前进的例子。
我正在尝试创建一个分段的 UITableView,它充当某种历史记录:它应该由一个由 HistoryBatchVO 对象组成的 NSMutableArray 填充。这些 HistoryBatchVO 对象包含一个时间戳 (NSDate) 和一个名称数组 (NSMutableArray),后者又包含 NameVO 类型的对象,其中包含(以及其他)一个 NSString。
我想使用时间戳作为部分标题,并将 NameVOs 中的字符串相应地填充到表中的部分中。
在我的表控制器中,我有:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataModel.history count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
(注意:_dataModel.history 是我的 NSMutableArray of HistoryBatchVO)。 ...但我想 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;
}
更新:解决问题后,工作代码如下:
-(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
标签: ios objective-c cocoa-touch uitableview