2012-06-29 19 views
15

Đây là vấn đề của tôi: Tôi có nhỏ UITableView này trong kịch bản của tôi: enter image description hereSet UITableView đại biểu và DataSource

Và đây là mã của tôi:

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 

Bây giờ bạn có thể thấy, tôi muốn đặt một cá thể được gọi là myTableDelegate làm Đại biểu và DataSource của myTable.

Đây là Nguồn của lớp 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 

tôi thực hiện tất cả các UITableViewDelegateUITableViewDataSource phương pháp mà các nhu cầu ứng dụng. Tại sao nó chỉ sụp đổ trước khi xem xuất hiện ??

Cảm ơn !!

+0

bạn có thể dán các lỗi? – JonLOo

+0

Bạn cũng có thể thêm nhật ký sự cố không? – rishi

+0

Kiểm tra cuộc thảo luận trong chuỗi - http://stackoverflow.com/questions/254354/uitableview-issue-when-using-separate-delegate-datasource – rishi

Trả lời

15

rickster là đúng. Nhưng tôi đoán bạn cần sử dụng vòng loại strong cho thuộc tính của mình kể từ khi kết thúc phương thức viewDidLoad đối tượng của bạn sẽ được deallocated anyway.

@property (strong,nonatomic) SmallTable *delegate; 

// inside viewDidload 

[super viewDidLoad]; 
self.delegate = [[SmallTable alloc] init];  
[self.myTable setDelegate:myTableDelegate]; 
[self.myTable setDataSource:myTableDelegate]; 

Nhưng có lý do nào để sử dụng đối tượng riêng biệt (nguồn dữ liệu và ủy nhiệm) cho bảng của bạn không? Tại sao bạn không đặt SmallViewController làm cả nguồn và đại biểu cho bảng của bạn?

Ngoài ra, bạn không tạo ô theo cách chính xác. Những dòng này không phải làm gì:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

dequeueReusableCellWithIdentifier chỉ đơn giản là lấy từ bảng "cache" một tế bào đó đã tạo ra và có thể được tái sử dụng (điều này để tránh tiêu thụ bộ nhớ) nhưng bạn chưa tạo bất kỳ.

Bạn đang làm ở đâu alloc-init?Làm điều này thay vì:

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

Hơn nữa nói với numberOfSectionsInTableView để trở về 1 thay vì 0:

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

Có lẽ bạn đang sử dụng ARC? myTableDelegate của bạn chỉ được tham chiếu trong biến cục bộ trong viewDidLoad - khi phương thức đó kết thúc, nó được phân phối lại. (Trong mẫu ủy quyền/nguồn dữ liệu, các đối tượng không sở hữu các đại biểu của họ, vì vậy tham chiếu của bảng xem lại đối tượng của bạn yếu.) Tôi không mong đợi điều đó gây ra sự cố, nhưng đó có thể là vấn đề quan trọng đối với vấn đề của bạn.

+0

OK, tôi vừa tạo một đại biểu mới @property (yếu, nonatomic) SmallTable *; Bây giờ ứng dụng không sụp đổ, nhưng ... xem bảng trống! Tôi không thể tìm ra lý do tại sao... –

1

setDelegate sẽ không giữ lại người được ủy quyền.

numberOfSectionsInTableView phải trả về 1 thay vì 0;

0

Các đại biểu của một đối tượng UITableView phải thông qua giao thức UITableViewDelegate. Các phương thức tùy chọn của giao thức cho phép đại biểu quản lý các lựa chọn, định cấu hình các tiêu đề phần và chân trang, giúp xóa các phương thức.

enter image description here

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

Số phận nên được đặt ít nhất một

Các vấn đề liên quan