【问题标题】:Set UITableView Delegate and DataSource设置 UITableView 委托和数据源
【发布时间】:2012-07-01 04:15:07
【问题描述】:

这是我的问题: 我的故事板中有这个小UITableView

这是我的代码:

SmallTableViewController.h

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

@interface SmallViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *myTable;

@end

SmallTableViewController.m

#import "SmallViewController.h"

@interface SmallViewController ()

@end

@implementation SmallViewController
@synthesize myTable = _myTable;

- (void)viewDidLoad
{
    SmallTable *myTableDelegate = [[SmallTable alloc] init];
    [super viewDidLoad];
    [self.myTable setDelegate:myTableDelegate];
    [self.myTable setDataSource:myTableDelegate];

    // Do any additional setup after loading the view, typically from a nib.
}

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

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

@end

现在如您所见,我想将一个名为 myTableDelegate 的实例设置为 myTable 的 Delegate 和 DataSource。

这是 SmallTable 类的来源。

SmallTable.h

#import <Foundation/Foundation.h>

@interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource>

@end

SmallTable.m

@implementation SmallTable

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 0;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    cell.textLabel.text = @"Hello there!";

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row pressed!!");
}

@end

我实现了应用程序需要的所有UITableViewDelegateUITableViewDataSource 方法。为什么它在视图出现之前就崩溃了??

谢谢!!

【问题讨论】:

标签: iphone objective-c uitableview delegates


【解决方案1】:

大概您正在使用 ARC?您的myTableDelegate 仅在viewDidLoad 的局部变量中被引用——一旦该方法结束,它就会被释放。 (在委托/数据源模式中,对象不拥有它们的委托,因此表视图对对象的引用很弱。)我不认为仅此一项会导致崩溃,但这可能是您问题的关键。

【讨论】:

  • 好的,我刚刚创建了一个新的@property (weak,nonatomic) SmallTable *delegate;现在应用程序没有崩溃,但是……表格视图是空的!我不知道为什么......
【解决方案2】:

setDelegate 不会保留委托。

numberOfSectionsInTableView 方法必须返回 1 而不是 0;

【讨论】:

    【解决方案3】:

    rickster 是对的。但我想您需要为您的属性使用 strong 限定符,因为在您的 viewDidLoad 方法结束时,无论如何都会释放对象。

    @property (strong,nonatomic) SmallTable *delegate;
    
    // inside viewDidload
    
    [super viewDidLoad];
    self.delegate = [[SmallTable alloc] init];    
    [self.myTable setDelegate:myTableDelegate];
    [self.myTable setDataSource:myTableDelegate];
    

    但是是否有任何理由为您的表使用单独的对象(数据源和委托)?为什么不将SmallViewController 设置为您的表的源和委托?

    此外,您没有以正确的方式创建单元格。这些行什么都不做:

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    cell.textLabel.text = @"Hello there!";
    

    dequeueReusableCellWithIdentifier 只是从表“缓存”中检索一个已创建并可重复使用的单元格(这是为了避免内存消耗)但您尚未创建任何单元格。

    alloc-init 你在哪里?改为这样做:

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell) {
        cell = // alloc-init here
    }
    // Configure the cell...
    cell.textLabel.text = @"Hello there!";
    

    再告诉numberOfSectionsInTableView返回1而不是0:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    

    【讨论】:

      【解决方案4】:

      UITableView 对象的委托必须采用 UITableViewDelegate 协议。协议的可选方法允许委托管理选择、配置部分标题和页脚、帮助删除方法。

      【讨论】:

        【解决方案5】:
        (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
        {
            // Return the number of sections.
            return 0;
        }
        

        部分的数量应至少设置为一个

        【讨论】:

          猜你喜欢
          • 2015-11-05
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 1970-01-01
          • 2014-01-04
          • 1970-01-01
          • 1970-01-01
          • 2014-08-08
          相关资源
          最近更新 更多