Ios 识别并加载/重新加载特定的tableView
我在故事板上设置了一个viewcontroller,并在该视图控制器中插入了一个tableView。我想做一个[self.tableView reloadData] 这就是我的viewController.m的外观。我的tableView是一个名为sharedView的IBOutlet,因此在方法中使用了名称,但我在调用configureView时在viewDidLoad上做了一些错误的事情,随后调用了Ios 识别并加载/重新加载特定的tableView,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在故事板上设置了一个viewcontroller,并在该视图控制器中插入了一个tableView。我想做一个[self.tableView reloadData] 这就是我的viewController.m的外观。我的tableView是一个名为sharedView的IBOutlet,因此在方法中使用了名称,但我在调用configureView时在viewDidLoad上做了一些错误的事情,随后调用了[self.sharedView reloadData]数据不会显示在表中 #impo
[self.sharedView reloadData]代码>数据不会显示在表中
#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
您提到您使用的是UIViewController,而不是UITableViewController。如果您使用的是UIViewController,则需要实现UITableViewDelegate和UITableViewDataSourceDelegate。您还需要通过代码或使用界面生成器或故事板连接这些代理 您应该添加有关VC正在实施的协议的信息:
@interface DetailViewController () <UITableViewDataSource, UITableViewDelegate>
- (void)configureView;
@end
您还可以直接在脚本中设置数据源和委托-我认为您必须按住ctrl键并将其从表视图拖动到vc。对不起,如果这是不正确的-我不使用IB有相当一段时间没有唯一的代码
还有一件事——读一下:
并将方法名称更改为与协议中的名称完全相同-如果您不这样做,它将无法工作
要获得更好的非持久数据源和代理,请尝试:
这些UITableView委托方法以“shareView”而不是“tableView”开头,这是怎么回事?是的,我想我把它们弄混了。我到底做错了什么?我到底是怎么做的?
- (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);
}
}