ios – 如何使用对象数组填充TableView?

我有一个TableView控制器,我想用数组中的对象填充.我正在使用StoryBoard.另外,我不确定是否需要将标签放在故事板中的Cell Prototype中作为一种占位符?

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

-班级名称

-AssignmentTitle

-截止日期

这是我现在拥有的

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

解决方法

我有类似的实现.

执行:
我创建了以下方法. (我已编辑它们以适合您提供的代码.)

-(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对象,因为我的猜测是你可能有不同的对象具有相同的className变量.

希望这可以帮助!

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...