菜鸟教程小白 发表于 2022-12-13 12:37:01

ios - UITableViewDelegate 没有被调用


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

<p>列出头文件:</p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;
#import &#34;tank.h&#34;

@class iTanksV2ListViewController;
@protocol iTanksV2ListViewControllerDelegate
   - (void) iTanksListViewController:(iTanksV2ListViewController *) sender choseTank:(tank *)tank;
@end

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

<p>和m文件:</p>

<pre><code>#import &#34;iTanksV2ListViewController.h&#34;
#import &#34;tank.h&#34;
#import &#34;tankDetailViewController.h&#34;

@interface iTanksV2ListViewController ()

@end

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


- (id)initWithStyle:(UITableViewStyle)style
{
    self = ;
    if (self) {
      // Custom initialization
    }
    return self;
}

- (void)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
{
    ;
    ;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @&#34;Tank List Table Cell&#34;;
    UITableViewCell *cell = ;
    if (!cell)
    {
      cell = [ initWithFrame:CGRectZero];
    }
    tank *thisTank = ;
    cell.textLabel.text = thisTank.tankNumber;
    return cell;
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if()
    {

    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tank *thisTank = ;
    ;

}

@end
</code></pre>

<p>以及接收文件的头部:</p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;
#import &#34;tankGauge.h&#34;
#import &#34;tank.h&#34;

@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
</code></pre>

<p>...和m文件:</p>

<pre><code>#import &#34;tankDetailViewController.h&#34;
#import &#34;iTanksV2ListViewController.h&#34;

@interface tankDetailViewController () &lt;iTanksV2ListViewControllerDelegate&gt;

@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 = ;
    if (self) {
      // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    ;
}

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

- (void)viewDidUnload
{
    ;
    ;
    ;
    ;
    ;
    ;
    // Release any retained subviews of the main view.
}

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

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>tankTableView</code> 是一个 <code>IBOutlet</code>,因此您只需在 <code> 中将 tableView 的委托(delegate)和数据源连接到您的 <code>File's Owner</code> >xib</code>如下图:
<img src="/image/XPRQI.png" alt="enter image description here"/> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewDelegate 没有被调用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9869769/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9869769/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewDelegate 没有被调用