• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableViewDelegate 没有被调用

[复制链接]
菜鸟教程小白 发表于 2022-12-13 12:37:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试从 UITableView 列表中调用详细信息屏幕 - 但未在接收 View 中调用委托(delegate) - 我将发布所有代码:

列出头文件:

#import <UIKit/UIKit.h>
#import "tank.h"

@class iTanksV2ListViewController;
@protocol iTanksV2ListViewControllerDelegate
     - (void) iTanksListViewControlleriTanksV2ListViewController *) sender choseTanktank *)tank;
@end

@interface iTanksV2ListViewController : UITableViewController
@property (nonatomic, strong) NSArray *tanks;
@property (weak, nonatomic) IBOutlet UITableView *tankTableView;
@property (weak, nonatomic) id <iTanksV2ListViewControllerDelegate> delegate;
@end

和m文件:

#import "iTanksV2ListViewController.h"
#import "tank.h"
#import "tankDetailViewController.h"

@interface iTanksV2ListViewController () 

@end

@implementation iTanksV2ListViewController
@synthesize tanks = _tanks;
@synthesize tankTableView = _tankTableView;
@synthesize delegate = _delegate;


- (id)initWithStyleUITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this    view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [self setTankTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    // Return the number of sections.
    return 1;//keep this section in case we do need to add sections in the future.
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    // Return the number of rows in the section.
    return [self.tanks count];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Tank List Table Cell";
    UITableViewCell *cell = [self.tankTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
    }
    tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
    cell.textLabel.text = thisTank.tankNumber;
    return cell;
}

-(void) prepareForSegueUIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString"Show Tank Details"])
    {

    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
    [self.delegate iTanksListViewController:self choseTank:thisTank];

}

@end

以及接收文件的头部:

#import <UIKit/UIKit.h>
#import "tankGauge.h"
#import "tank.h"

@interface tankDetailViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *tankNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *tankProductLabel;
@property (weak, nonatomic) IBOutlet UILabel *tankAvailableProductLabel;
@property (weak, nonatomic) IBOutlet UILabel *tankMaxVolumeLabel;
@property (weak, nonatomic) IBOutlet tankGauge *tankVolumeGauge;
@property (weak, nonatomic)  tank* tankToShow;
@end

...和m文件:

#import "tankDetailViewController.h"
#import "iTanksV2ListViewController.h"

@interface tankDetailViewController () <iTanksV2ListViewControllerDelegate>

@end

@implementation tankDetailViewController
@synthesize tankNumberLabel = _tankNumberLabel;
@synthesize tankProductLabel = _tankProductLabel;
@synthesize tankAvailableProductLabel = _tankAvailableProductLabel;
@synthesize tankMaxVolumeLabel = _tankMaxVolumeLabel;
@synthesize tankVolumeGauge = _tankVolumeGauge;
@synthesize tankToShow = _tankToShow;


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

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)iTanksListViewController:(iTanksV2ListViewController *)sender choseTank:(id)tank
{
    self.tankToShow = tank;
   self.tankNumberLabel.text = self.tankToShow.tankNumber;
}

- (void)viewDidUnload
{
    [self setTankNumberLabel:nil];
    [self setTankProductLabel:nil];
    [self setTankAvailableProductLabel:nil];
    [self setTankMaxVolumeLabel:nil];
    [self setTankVolumeGauge:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end



Best Answer-推荐答案


tankTableView 是一个 IBOutlet,因此您只需在 中将 tableView 的委托(delegate)和数据源连接到您的 File's Owner >xib如下图: enter image description here

关于ios - UITableViewDelegate 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9869769/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap