gpt4 book ai didi

ios - 识别然后加载/重新加载特定的 tableView

转载 作者:行者123 更新时间:2023-11-28 22:12:02 24 4
gpt4 key购买 nike

我在 Storyboard上设置了一个 View Controller ,并在该 View Controller 中插入了一个 tableView。我想做一个 [self.tableView reloadData]。

这就是我的 viewController.m 的样子。我的 tableView 是一个名为 sharedView 的 IBOutlet,因此在方法中有名称,但是当我调用 configureView 时我在 viewDidLoad 上做错了,随后 [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

最佳答案

您应该添加有关您的 VC 正在实现的协议(protocol)的信息:

@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);
}
}

您还可以直接在 Storyboard 中设置数据源和委托(delegate) - 我认为您必须按住 ctrl 并从 TableView 拖动到 vc。抱歉,如果这不正确 - 我已经有一段时间不使用 IB 了 - 只有代码。

还有一件事——读这个: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource并将方法名称更改为与协议(protocol)中的名称完全相同 - 如果您不这样做,它将无法工作。

为了更好地理解数据源和委托(delegate),请尝试: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html

关于ios - 识别然后加载/重新加载特定的 tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22670396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com