gpt4 book ai didi

ios - Storyboard中的新 View Controller 中未调用 TableView 委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-29 03:21:47 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 Storyboard。在 Storyboard中,我使用默认 View Controller 进行登录,并创建了另一个 View Controller 名称 Inventory。在新的 View Controller 中,我使用 TableView 。该表正在显示,但未调用委托(delegate)和数据源方法。我尝试了很多解决方案,但我不明白这个问题。我使用的代码是:

#import <UIKit/UIKit.h>

@interface InventoryViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

实现文件是:

 #import "InventoryViewController.h"
#import "customCell.h"
@interface InventoryViewController ()

@end

@implementation InventoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 110, 320, 350)];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
[table relaodData];

}
- (IBAction)Logout:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
[self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"hello");
return 5;

}



- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customcell"];

if (cell == nil)
{
cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"customcell"];
}

// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
cell.item_image.image = [UIImage imageNamed:@"ipad_strip_logo.png"];
cell.type.text = @"electronics";
cell.name.text = [NSString stringWithFormat:@"my object %d",indexPath.row];

return cell;
}
@end

最佳答案

在设置deledate、datasource和datasource数组后需要应用-reloadData:方法。

重新加载数据

重新加载接收器的行和部分。-(无效)重新加载数据讨论

调用此方法可重新加载用于构建表格的所有数据,包括单元格、节页眉和页脚、索引数组等。为了提高效率, TableView 只重新显示那些可见的行。如果表因重新加载而缩小,它会调整偏移量。当 TableView 的委托(delegate)或数据源希望 TableView 完全重新加载其数据时调用此方法。不应在插入或删除行的方法中调用它,尤其是在通过调用 beginUpdates 和 endUpdates 实现的动画 block 中

浏览此文档 https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadData

关于ios - Storyboard中的新 View Controller 中未调用 TableView 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993286/

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