ios 如何使用对象数组填充 TableView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19529137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate a TableView with an Array of Objects?
提问by Mike
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?
我有一个 TableView 控制器,我想用数组中的对象填充它。我正在使用故事板。另外,我不确定是否需要将标签放在情节提要中的 Cell Prototype 中作为一种占位符?
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):
我的 aList 可变数组包含 Homework 类型的对象。在表格的每一行中,我想显示(这些是我的作业模型中已经设置的变量):
-ClassName
-班级名称
-AssignmentTitle
-作业标题
-DueDate
-到期日
Here is what I currently have
这是我目前拥有的
TableViewController.h
表视图控制器.h
@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>
@property(strong,nonatomic) Assignment *vc;
@property (strong, nonatomic) NSMutableArray *alist;//array was filled with delegation
@end
TableViewController.m
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;
}
回答by faterpig
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:
在 UITableViewController 委托方法中:
- (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.
使用 homeworkAtIndex 方法将允许您将数组中的不同对象填充到表中的不同单元格中。
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.
我在这里使用 NSString 来检查 className,因为我不确定 Homework 对象是什么,但是您绝对可以更改逻辑以适合 Homework 对象,因为我猜测您可能有具有相同 className 变量的不同对象。
Hope this helps! :)
希望这可以帮助!:)
回答by Obj-Swift
There are Plenty of tutorials out there which populates tableviews using arrays. Take a look at these tutorials.
有很多使用数组填充表格视图的教程。看看这些教程。
http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
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
http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1
you will need to implement custom cell to accommodate 3 labels.. default table cells usually have 1 image view and two labels..
您将需要实现自定义单元格以容纳 3 个标签.. 默认表格单元格通常有 1 个图像视图和两个标签..
回答by manuelBetancurt
use a custom cell
使用自定义单元格
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
use the cell as a view, just position your labels on the content view of the cell[1] I see you already have the object with the data you want to display, so use a cell with the things you want to show
将单元格用作视图,只需将标签放在单元格的内容视图上[1] 我看到您已经拥有包含要显示的数据的对象,因此请使用包含要显示的内容的单元格