如何填充对象数组一个TableView中? [英] How to populate a TableView with an Array of Objects?

查看:285
本文介绍了如何填充对象数组一个TableView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想从一个数组对象来填充的TableView控制器。我使用的故事板。另外,我不知道如果我需要放置标签的电池原型故事板作为一种占位符的?

我ALIST可变数组包含类型作业对象。
在表的每一行,我要显示(在我的作业模式已经设置这些变量):

-ClassName

-AssignmentTitle

-DueDate

下面是我目前有

TableViewController.h

  @interface AssignmentTableController:的UITableViewController
<
AssignmentDelegate
>@property(强,非原子)转让* VC;
@property(强,非原子)的NSMutableArray * ALIST; //数组中弥漫着代表团
@结束

TableViewController.m

 的#pragma马克 - 表视图的数据源 - (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView
{//返回部分的数量。
返回1;
} - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分
{//返回部分中的行数。
 返回[self.alist计数]
} - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
静态的NSString * CellIdentifier = @细胞;
* UITableViewCell的细胞= [的tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
作业*艾= [self.alist objectAtIndex:indexPath.row];// *******,我不知道该怎么从这里做******
// *******我要开始显示每行1对象,显示什么上面说过***  //配置单元...返回细胞;
}


解决方案

我有一个类似的实现。

实施:
我创建了下面的方法。 (我已经编辑它们以满足您的code提供。)

   - (作业*)homeworkAtIndex:(NSInteger的)指数
{
    返回[ALIST objectAtIndex:指数]
} - (NSInteger的)numberOfObjectsInList
{
    返回[ALIST计数]
}

和中的UITableViewController委托方法:

   - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分

我用这个方法调用

 返回[个体经营numberOfObjectsInList]

在委托方法:

   - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    静态的NSString * CellIdentifier = @细胞;
    * UITableViewCell的细胞= [的tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    电池= [[[UITableViewCell的页头] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SubtitleIdentifier]自动释放];
    作业* AI = [自我homeworkAtIndex:indexPath.row];    / *你的其他单元配置
    cell.textLabel.text = ai.className; //如。文字的显示名称
    cell.detailTextLabel.text = [的NSString stringWithFormat:@%@ |%@,ai.assignmentTitle,ai.dueDate] //将显示与细节文本的单元格作业标题|截止日期,例如。 实验1 | 2013年10月23日被称为物理一个className出现下
    * /
}

使用homeworkAtIndex将允许你数组中的不同对象填充到表格中的不同小区的方法

这种方式让我过具有创建自定义单元格和格式化的单元尺寸,以适应表。这也许可以为你工作,如果是将要显示的数据并不长,并没有真正必须使用定制单元。 (就像我提供的例子)

如果你想知道如何检查选择的不同的单元格(如你可能希望此后他们推到一个不同的viewController),你可以在委托方法做到这一点:

   - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath
{
    *的UIViewController = nextViewController零;
    * NSString的cellDisplayName = [代表homeworkAtIndex:indexPath.row]。名称; //可以是作业对象,如果你想
    如果([cellDisplayName isEqualToString:[个体经营homeworkAtIndex:0机] .className])
        nextViewController = [[firstViewController的alloc]初始化];
    否则,如果([cellDisplayName isEqualToString:[个体经营homeworkAtIndex:1机] .className])
        nextViewController = [[secondViewController的alloc]初始化];
    //推移为取决于阵列中的项目数的检查
}

我使用的NSString在这里检查的className因为我不知道该作业对象是什么,但你绝对可以改变的逻辑,以适应家庭作业对象,我的猜测是,你可能有不同的对象具有相同的className变量。

希望这有助于! :)

I have a TableView Controller that I want to populate with Objects from an array. I am using StoryBoard. Also, I am not sure if I need to place Labels in the Cell Prototype in storyboard as a type of placeholder?

My aList Mutable Array contains objects of type Homework. In each row of the table, I want to display (These are variables already set in my Homework Model):

-ClassName

-AssignmentTitle

-DueDate

Here is what I currently have

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

解决方案

I have a similar implementation.

Implementation: I created the following methods. (I have edited them to fit your code provided.)

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

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

And in the UITableViewController delegate method:

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

I used this method call

return [self numberOfObjectsInList]; 

In the delegate method:

- (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"
    */
}

Using the method homeworkAtIndex will allow you to populate the different objects in the array into the different cells in the table.

This way got me through having to create custom cells and formatting the cell sizes to fit the table. Maybe this can work for you if the data that is going to be shown is not that long, and does not really have to use the custom cells. (Much like the example I provided)

If you were wondering how to check for different cells selected (as you may wish to push them to a different viewController thereafter), you can do this in the delegate method:

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

I am using NSString here to check for the className as I'm not sure what the Homework object is, but you can definitely change the logic to fit the Homework object, as my guess is you may have different objects with the same className variable.

Hope this helps! :)

这篇关于如何填充对象数组一个TableView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆