【问题标题】:Populating a sectioned UITableView填充分段的 UITableView
【发布时间】: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


【解决方案1】:

假设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;
}

【讨论】:

  • 感谢您的帮助!我会尽量适应你的例子!但是 numberOfRowsInSection 现在会引发错误:[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
  • 如果你的 numberOfSectionsInTableView 返回 [_dataModel.history count],那么 numberOfRowsInSection 方法应该总是在 [_dataModel.history objectAtIndex:section] 处得到一些东西。这可能有问题。
  • 当我注销 _dataModel.history 的计数时,它显示 4 是正确的。在 numberOfRowsInSection 中记录部分编号,它显示 3, 0, 1, 2 ... 所以它们不按顺序排列。这可能与错误有关吗?
  • 这取决于表格单元格的绘制和重用顺序。 UITableView 是一个滚动视图,当您通过触摸滚动时,屏幕上会绘制不同的部分。 NSLog ("%@", _dataModel.history) 来查看那里的元素。
  • 万岁它现在工作了!该错误不是由 numberOfRowsInSection 引起的,而是由 titleForHeaderInSection 引起的:我不小心使用了 HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.row];
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多