我有一个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变量.
希望这可以帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。