Ios 填充分区UITableView
好的,这个问题可能已经被问过好几次了,但是我找不到一个能帮助我前进的例子 我试图创建一个分区UITableView,它充当某种历史:它应该由一个由HistoryBatchVO对象组成的NSMutableArray填充。这些HistoryBatchVO对象包含一个时间戳(NSDate)和一个名称数组(NSMutableArray),后者依次包含一个包含NSString的NameVO类型的对象 我想使用时间戳作为节头,并将NameVOs中的字符串相应地填充到表中的节中 在我的表格控制器中,我有:Ios 填充分区UITableView,ios,objective-c,cocoa-touch,uitableview,Ios,Objective C,Cocoa Touch,Uitableview,好的,这个问题可能已经被问过好几次了,但是我找不到一个能帮助我前进的例子 我试图创建一个分区UITableView,它充当某种历史:它应该由一个由HistoryBatchVO对象组成的NSMutableArray填充。这些HistoryBatchVO对象包含一个时间戳(NSDate)和一个名称数组(NSMutableArray),后者依次包含一个包含NSString的NameVO类型的对象 我想使用时间戳作为节头,并将NameVOs中的字符串相应地填充到表中的节中 在我的表格控制器中,我有: -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataModel.history count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
(注意:_dataModel.history是HistoryBatchVOs的NSMutableArray)。
... 但是我想,numberOfRowsInSection需要返回HistoryBatchVO.names数组中的对象数。问题是我如何做到这一点
另外,如何更改cellforrowatinexpath的实现以使其正常工作
-(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];
}
您还需要实现标题things部分,但我认为您会喜欢使用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;
}
NSLog
\u dataModel.description
感谢您的帮助!我将尝试改编你的例子!但是numberOfRowsInSection现在抛出一个错误:[uu NSArrayM objectAtIndex:]:索引3超出边界[0..2]如果NumberOfSectionsTableView返回[\u dataModel.history count],则numberOfRowsInSection方法应始终在[\u dataModel.history objectAtIndex:section]获取某些内容。这可能有问题。当我注销我的_dataModel.history的计数时,它显示4,这是正确的。将节号记录在numberOfRowsInSection中,它显示3、0、1、2。。。因此,它们不符合规程。这可能与错误有关吗?这取决于表格单元格的绘制和重用顺序。UITableView是一个滚动视图,当您使用触摸滚动时,屏幕上会绘制不同的部分。NSLog(“%@”,_dataModel.history)来查看那里的元素。万岁!它正在工作!该错误不是由numberOfRowsInSection引起的,而是由titleForHeaderInSection引起的:我不小心使用了HistoryBatchVO*batchVO=[\u dataModel.history objectAtIndex:indexath.row];