簡體   English   中英

用UITableView子類化UIViewController

[英]Subclassing a UIViewController with a UITableView

我正在嘗試對具有嵌入式UITableView且是UiTableViewDataSource和UITableViewDelegate的UIViewController進行子類化。

大師班看起來像這樣:

@interface PFUIViewControllerWithTable : PFUIViewController     <UITableViewDelegate,UITableViewDataSource, RKObjectLoaderDelegate> {
UITableView *_tableView;
NSArray *_data;
}
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSArray *data;
- (void)configureCell:(PFRewardsUITableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath;

該實現包含用於從self.data屬性填充表視圖的方法。

在子類中,填充數據並調用[self.tableView refresh]

@interface MySubclass : PFUIViewControllerWithTable <UITableViewDataSource,     UITableViewDelegate> {

}
- (void)loadObjectsFromDataStore;
- (void)loadLiveData;
- (void)configureCell:(PFRewardsUITableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath;

在此配置中永遠不會調用UItableViewDataSource方法。 委托和數據源設置為master類。 主類從self.data屬性填充表視圖,該屬性由子類修改。

那么,如何繼承此類視圖呢?

使用IBOulet進行表格瀏覽

現在,在視圖控制器的xib中檢查表視圖已綁定,並將其委托和數據源提供給文件所有者。

現在檢查您提供給tableview的數組中是否包含內容

確保您正確設置了委托和數據源。

這是個錯誤:

PFUIViewControllerWithTable *controller = [[MySubclass alloc] init];
[controller.tableView setDelegate:controller];
[controller.tableView setDataSource:controller];

這應該沒問題:

PFUIViewControllerWithTable *controller = [[MySubclass alloc] init];
[controller.tableView setDelegate:(MySubclass*)controller];
[controller.tableView setDataSource:(MySubclass*)controller];

我相信那是您的問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM