好吧,这个问题可能已经被问过几次了,但我找不到一个能帮助我继续前进的例子。
我正在尝试创建一个分段的 UITableView,它充当某种历史记录:它应该由一个由 HistoryBatchVO 对象组成的 NSMutableArray 填充。这些 HistoryBatchVO 对象包含一个时间戳 (NSDate) 和一个名称数组 (NSMutableArray),后者又包含 NameVO 类型的对象,其中包含一个 NSString。
我想使用时间戳作为节标题,并将 NameVO 中的字符串相应地填充到表中的节中。
在我的表 Controller 中我有:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataModel.history count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
(注意:_dataModel.history 是我的 HistoryBatchVO 的 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;
}
更新:解决问题后,这是工作代码:
-(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;
}
最佳答案
假设 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;
}
关于ios - 填充一个分段的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20561982/