我是 ios 编程的新手,所以如果问题很简单,请多多包涵。我有一个映射到表视图控制器的核心数据表。其中的数据目前如下所示 - 有一个原型单元:
我需要按日期汇总数据,并在不同部分显示每个日期的详细信息,总和作为第一行。就像是:
我的问题是这可行吗?我想我需要在每个表格单元格中创建部分和两个原型单元格。将不胜感激快速反馈。
谢谢大家!
我是 ios 编程的新手,所以如果问题很简单,请多多包涵。我有一个映射到表视图控制器的核心数据表。其中的数据目前如下所示 - 有一个原型单元:
我需要按日期汇总数据,并在不同部分显示每个日期的详细信息,总和作为第一行。就像是:
我的问题是这可行吗?我想我需要在每个表格单元格中创建部分和两个原型单元格。将不胜感激快速反馈。
谢谢大家!
执行此操作的简单方法是使用节标题。您可以使用单个字符串 ( @"%@: %@", date, total
) 或带有标签的包装视图,左侧为日期,右侧为总计。
-(NSString *) tableView:(UITableView *)tv titleForHeaderInSection:(NSInteger)s
{
NSString *dateString = [self dateStringForSection:s];
float total = [self totalForSection:s];
return [NSString stringWithFormat:@"%@: %0.2f", dateString, total];
}
或者
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [self wrappedHeaderForSection:s];
}
当然,您必须实施dateStringForSection:
或wrappedHeaderForSection:
适当地实施。
最简单的方法是将您的 UITableView 设置为“UITableViewStyleGrouped”。
UITableView *tab = [[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped];
或者您可以转到界面生成器并在表格视图中将样式从普通更改为分组。
“分组”样式将您的表格分成多个部分。
使用 UITableViewDelegate 方法指定所有参数。
// 告诉表中的节数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return numberOfSections;
}
//告诉每个section的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) { return 2; } else if(section == 1)...
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) { //Show Amount for Jul 02, 2013 cell.textLabel.text = @"Jul 02, 2013"; cell.detailTextLabel = @"20.35"; } // Do the same for all rows and section in table.
}
如需进一步参考 - http://mobisoftinfotech.com/iphone-uitableview-tutorial-grouped-table/
您还应该查看 Sensible TableView 框架。在使用 Core Data 时为我节省了大量时间。