ios - 填充一个分段的 UITableView

标签 ios objective-c cocoa-touch uitableview

好吧,这个问题可能已经被问过几次了,但我找不到一个能帮助我继续前进的例子。

我正在尝试创建一个分段的 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/

相关文章:

javascript - 通过 App Store 更新基于 Cordova/Phonegap 的应用程序 "crashes"

iphone - UIButton 渐变不起作用

iphone - 创建单个脂肪文件时遇到 lipo 错误

ios - 在 Swift 中使用 WillSpeakRange 委托(delegate)中断 AVSpeechUtterance 和恢复

ios - 如何访问另一个应用程序的 iCloud 容器(使用另一个开发人员配置文件开发的应用程序和在该配置文件中创建的 icloud 容器)?

iPhone : Getting a value back from views

ios - 在 Objective C 中通过按钮触摸传递 URL

ios - 呈现 iOS HealthKit 权限模态视图时,其后面的 View 为黑色

iphone - -[NSCFString objectAtIndex :]: unrecognized selector

android - GoogleAuthUtil 类似于 iOS 的设备身份验证,用于验证请求是否来自 Apple 设备