我在故事板上设置了一个viewcontroller,我在这个视图控制器中插入了一个tableView。我想做一个[self.tableView reloadData]。
这就是我的viewController.m的样子。我的tableView是一个名为sharedView的IBOutlet,因此是方法中的名称,但是当我调用configureView时,我在viewDidLoad上做错了,随后[self.sharedView reloadData];
数据没有显示在表中。< / p>
#import "DetailViewController.h"
#import "sharedUsersTable.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [[self.detailItem objectForKey:@"lock_name"] description];
activeUsers = [self.detailItem objectForKey:@"active_shared_users"];
[self.sharedView reloadData];
//NSLog(@"Info: %@", activeUsers);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSString *)sharedView:(UITableView *)sharedView titleForHeaderInSection:(NSInteger)section {
return @"Shared with";
}
- (NSInteger)sharedView:(UITableView *)sharedView numberOfRowsInSection:(NSInteger)section
{
return activeUsers.count;
}
- (sharedUsersTable *)sharedView:(UITableView *)sharedView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"sharedUser";
sharedUsersTable *cell = [sharedView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[sharedUsersTable alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *key;
NSString *name;
NSString *email;
// NSString *permission;
key = [activeUsers objectAtIndex:indexPath.row];
name = [key objectForKey:@"shared_user_name"];
email = [key objectForKey:@"email"];
NSLog(@"Info: %@", name);
cell.textLabel.text = [NSString stringWithFormat:@"%@", name];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", email];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
@end
答案 0 :(得分:2)
您应该添加有关您的VC正在实施的协议的信息:
@interface DetailViewController () <UITableViewDataSource, UITableViewDelegate>
- (void)configureView;
@end
然后:
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [[self.detailItem objectForKey:@"lock_name"] description];
activeUsers = [self.detailItem objectForKey:@"active_shared_users"];
/* I assume that Your table view is self.sharedView though You should change the name and I assume that it is connected to VC */
self.sharedView.dataSource = self;
self.sharedView.delegate = self;
[self.sharedView reloadData];
//NSLog(@"Info: %@", activeUsers);
}
}
您还可以直接在故事板中设置数据源和委托 - 我认为您必须从表视图中按住Ctrl键并拖动到vc。对不起,如果这不正确 - 我不会使用IB很长一段时间没有 - 只有代码。
还有一件事 - 读这个: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource 并将方法名称更改为与协议中的完全相同 - 如果您不这样做,它就不会工作。
对于更好的不受欢迎的数据源和代理,请尝试: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html
答案 1 :(得分:0)
您提到您使用的是UIViewController而不是UITableViewController。如果您正在使用UIViewController,那么您将需要实现UITableViewDelegate和UITableViewDataSourceDelegate。您还需要通过代码或使用界面构建器或Storyboard连接这些代理。