【问题标题】:table view delegate methods not being called in new view controller in storyboards未在情节提要的新视图控制器中调用表视图委托方法
【发布时间】:2014-01-08 19:09:42
【问题描述】:

在我的应用程序中,我正在使用情节提要。在情节提要中,我使用默认视图控制器进行登录并创建了另一个视图控制器名称 Inventory。在新的视图控制器中,我使用的是表格视图。该表正在显示,但未调用委托和数据源方法。我尝试了很多解决方案,但我无法理解问题。我使用的代码是:

#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

【问题讨论】:

  • 运行项目时日志中是否打印了“hello”?
  • @Ashu no hello 不打印,单元格数也不是 10..

标签: ios objective-c uitableview


【解决方案1】:

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

重新加载数据

重新加载接收器的行和部分。 - (void)reloadData 讨论

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

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

【讨论】:

  • 我已经添加了那个方法,仍然没有调用委托方法并且一个空表来了
  • @BalaChandra,更新您的问题最新代码并检查自定义电池插座是否正确配置/如果以编程方式检查代码。
  • Customecell 已配置,但即使我已将单元格数指定为 10,并且在该方法中我使用了 nsLog(@"hello") 它也没有打印单元格数不是 10
  • Dude NSLog 语句不会执行,因为您之前从该函数返回。
猜你喜欢
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 2011-06-26
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多