【问题标题】:How to populate a TableView with an Array of Objects?如何用对象数组填充 TableView?
【发布时间】:2013-10-31 23:43:27
【问题描述】:

我有一个 TableView 控制器,我想用数组中的对象填充它。我正在使用故事板。另外,我不确定是否需要在情节提要的 Cell Prototype 中放置 Labels 作为占位符类型?

我的 aList 可变数组包含 Homework 类型的对象。 在表格的每一行中,我想显示(这些是我的作业模型中已经设置的变量):

-类名

-作业标题

-截止日期

这是我目前拥有的

TableViewController.h

@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>

@property(strong,nonatomic) Assignment *vc;
@property (strong, nonatomic) NSMutableArray *alist;//array was filled with delegation 


@end

TableViewController.m

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
 return [self.alist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier     forIndexPath:indexPath];
Homework *ai = [self.alist objectAtIndex:indexPath.row];

//*******I am not sure what to do from Here******
//*******I need to start displaying 1 object per row,displaying what was stated above***

  // Configure the cell...

return cell;
}

【问题讨论】:

标签: ios objective-c arrays uitableview


【解决方案1】:

我有一个类似的实现。

实施: 我创建了以下方法。 (我已经对它们进行了编辑以适合您提供的代码。)

-(Homework*) homeworkAtIndex: (NSInteger) index
{
    return [alist objectAtIndex:index] ;
}

-(NSInteger) numberOfObjectsInList
{
    return [alist count];
}

并且在 UITableViewController 委托方法中:

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section

我用这个方法调用

return [self numberOfObjectsInList]; 

在委托方法中:

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SubtitleIdentifier] autorelease];
    Homework *ai = [self homeworkAtIndex: indexPath.row];

    /* your other cell configurations
    cell.textLabel.text = ai.className; // eg. display name of text
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ | %@",ai.assignmentTitle,ai.dueDate]; // will show the cell with a detail text of "assignment title | due date" eg. "Lab 1 | 23 Oct 2013" appearing under the className called "Physics"
    */
}

使用方法 homeworkAtIndex 将允许您将数组中的不同对象填充到表格中的不同单元格中。

这种方式让我不必创建自定义单元格并格式化单元格大小以适合表格。如果要显示的数据不是那么长,并且实际上不必使用自定义单元格,那么这可能对您有用。 (很像我提供的例子)

如果您想知道如何检查选择的不同单元格(因为您可能希望随后将它们推送到不同的 viewController),您可以在委托方法中执行此操作:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
    UIViewController* nextViewController = nil;
    NSString* cellDisplayName = [delegate homeworkAtIndex: indexPath.row].name; // can be the Homework object if you want
    if( [cellDisplayName isEqualToString:[self homeworkAtIndex:0].className] )
        nextViewController = [[firstViewController alloc] init];
    else if( [cellDisplayName isEqualToString:[self homeworkAtIndex:1].className] )
        nextViewController = [[secondViewController alloc] init];
    // goes on for the check depending on the number of items in the array
}

我在这里使用 NSString 来检查 className,因为我不确定 Homework 对象是什么,但你绝对可以更改逻辑以适应 Homework 对象,因为我猜你可能有不同的对象具有相同的类名变量。

希望这会有所帮助! :)

【讨论】:

    【解决方案2】:

    那里有大量使用数组填充表格视图的教程。看看这些教程。

    http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

    http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1

    您需要实现自定义单元格以容纳 3 个标签。默认表格单元格通常有 1 个图像视图和两个标签。..

    【讨论】:

      【解决方案3】:

      使用自定义单元格

      http://www.appcoda.com/customize-table-view-cells-for-uitableview/

      将单元格用作视图,只需将标签放在单元格的内容视图上[1] 我看到您已经有了包含要显示的数据的对象,因此请使用包含您要显示的内容的单元格

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        • 2019-01-15
        • 1970-01-01
        相关资源
        最近更新 更多