我有一个 TableView 控制器,我想用数组中的对象填充它。 我正在使用故事板。另外,我不确定是否需要将标签作为占位符放置在情节提要的单元格原型中?
我的 aList 可变数组包含家庭作业类型的对象。在表格的每一行中,我想显示(这些是在我的家庭作业模型中已经设置的变量):
-类名
-作业标题
-到期日
这是我目前拥有的
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,因为我不确定家庭作业对象是什么,但您绝对可以更改逻辑以适应家庭作业对象,因为我的猜测是您可能有不同的对象具有相同的 className 变量。
希望这有帮助! :)
教程使用数组填充表视图。看看这些教程。
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 个图像视图和两个标签。
使用自定义单元格
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
使用单元格作为视图,只需将标签放在单元格的内容视图上[1]我看到您已经拥有包含要显示的数据的对象,因此请使用包含要显示的内容的单元格