6

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

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

-班级名称

-作业标题

-到期日

这是我目前拥有的

表视图控制器.h

@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>

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


@end

表视图控制器.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;
}
4

3 回答 3

7

我有一个类似的实现。

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

-(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 变量的不同对象。

希望这可以帮助!:)

于 2013-10-23T01:57:45.297 回答
3

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

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 个图像视图和两个标签。

于 2013-10-22T22:02:28.990 回答
0

使用自定义单元格

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

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

于 2013-10-22T22:01:48.050 回答