按照以下教程,我没有遇到任何问题:http ://www.raywenderlich.com/1797/how-to-create-a-simple-iphone-app-tutorial-part-1 。从本质上讲,本教程教人们创建一个简单的表格视图,其中填充来自 NSMutableArray 的数据,然后深入到每个项目的 DetailView 中。
然后,我尝试通过创建 TabViewController 并将创建的表视图作为此控制器的“选项卡项目 2”来增强我所拥有的功能。
这打破了表格的自动填充。
用于从AppDelegate.m填充表的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Person *person1 = [[Person alloc] initWithName:@"Monica"
balance:420
thumbImage:[UIImage imageNamed:@"01.jpg"]
fullImage:[UIImage imageNamed:@"01_t.jpg"]];
Person *person2 = [[Person alloc] initWithName:@"Ross"
balance:630
thumbImage:[UIImage imageNamed:@"02.jpg"]
fullImage:[UIImage imageNamed:@"02_t.jpg"]];
Person *person3 = [[Person alloc] initWithName:@"Rachel"
balance:420
thumbImage:[UIImage imageNamed:@"03.jpg"]
fullImage:[UIImage imageNamed:@"03_t.jpg"]];
NSMutableArray *persons = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *initialViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
self.window.rootViewController = initialViewController;
TableViewController * peopleTableViewController = [storyboard instantiateViewControllerWithIdentifier:@"PersonTableViewController"];
[peopleTableViewController setPersons: persons];
// Override point for customization after application launch.
return YES;
}
'persons' 是一个 Person 实例数组,它是 TableViewController 的一个属性。以下是基于TableViewController.m中的 'persons' 数组的表格人口:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PersonCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//For each person
Person *person = [self.persons objectAtIndex:indexPath.row];
cell.textLabel.text = person.name;
cell.imageView.image = person.thumbImage;
cell.detailTextLabel.text = [person getPrice];
return cell;
}
最后,故事板位于:http: //i.stack.imgur.com/9axs8.png
只是为了重新迭代,这一切都编译得很好,但是在测试期间,打开选项卡项目 #2 时表是空的(根据情节提要,表视图控制器存在于该表中)。这在我添加标签之前有效。
我对Objective C还是很陌生,所以如果您需要任何其他信息,请告诉我。