UITableViewController 通过方法添加项目
UITableViewController add item via method
我正在使用 UISplitviewController,我正在尝试将项目添加到 table 视图。
现在我有两个办法
创建一个按钮并添加它:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
单击按钮时运行此代码:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
并将项目添加到 table 视图:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
这种方式可行。
这就是我想要做的:
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
但这不会更新 table 视图。我添加了一个断点,当我使用我添加的按钮时,它进入 table 视图,使用它没有的方法,我通过 [MasterController GetRequest];
调用该方法
我做错了什么?
我正在从另一个控制器调用 GetRequest。
MasterController 的定义方式如下:
@interface DetailController ()
{
MasterController *MasterController;
}
DetailController.m:
#import "MasterController.h"
@interface DetailController ()
{
MasterController *MasterController;
}
@end
@implementation DetailController
-(void)viewDidLoad {
MasterController = [[MasterController alloc]init];
}
[MasterController GetRequest];
MasterController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}
UITableViewController
并不是那样的。
如果你将它子类化,你需要实现几个方法来告诉 table 它有什么数据 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
要添加新行,您只需将对象添加到 self.objects
并调用 [self.tableView reloadData];
基本上是 -
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}
正如我所怀疑的那样,通过这一行 MasterController = [[MasterController alloc]init]
,您正在创建一个与您在屏幕上看到的实例无关的新实例。您需要获得对拆分视图控制器中已有的主控制器的引用。从详细视图控制器中,你可以这样得到,
MasterController = self.splitViewController.viewControllers.firstObject;
split view controller 有一个viewControllers 属性,index 0 的是master,index 1 的是detail。顺便说一句,您应该以 class 小写字母开头您的 ivars 和方法名称。
我正在使用 UISplitviewController,我正在尝试将项目添加到 table 视图。
现在我有两个办法
创建一个按钮并添加它:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; self.navigationItem.rightBarButtonItem = addButton;
单击按钮时运行此代码:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
并将项目添加到 table 视图:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
这种方式可行。
这就是我想要做的:
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
但这不会更新 table 视图。我添加了一个断点,当我使用我添加的按钮时,它进入 table 视图,使用它没有的方法,我通过 [MasterController GetRequest];
我做错了什么?
我正在从另一个控制器调用 GetRequest。
MasterController 的定义方式如下:
@interface DetailController ()
{
MasterController *MasterController;
}
DetailController.m:
#import "MasterController.h"
@interface DetailController ()
{
MasterController *MasterController;
}
@end
@implementation DetailController
-(void)viewDidLoad {
MasterController = [[MasterController alloc]init];
}
[MasterController GetRequest];
MasterController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}
UITableViewController
并不是那样的。
如果你将它子类化,你需要实现几个方法来告诉 table 它有什么数据 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
要添加新行,您只需将对象添加到 self.objects
并调用 [self.tableView reloadData];
基本上是 -
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}
正如我所怀疑的那样,通过这一行 MasterController = [[MasterController alloc]init]
,您正在创建一个与您在屏幕上看到的实例无关的新实例。您需要获得对拆分视图控制器中已有的主控制器的引用。从详细视图控制器中,你可以这样得到,
MasterController = self.splitViewController.viewControllers.firstObject;
split view controller 有一个viewControllers 属性,index 0 的是master,index 1 的是detail。顺便说一句,您应该以 class 小写字母开头您的 ivars 和方法名称。