2010-01-19 58 views
8

典型的UITableView使用模式是讓主UIViewController成爲它所持有的UITableView的目標數據源和委託。從主UIViewController類中分離UITableview數據源和委託的簡單方法?

是否有任何簡單易用的教程可以幫助我弄清楚如何將與UITableViewDelegate和UITableViewDataSource方法相關的代碼移動到單獨的類中,並將其與我的UIViewController掛鉤?理想情況下,我希望將代表和數據源都放在同一個班級中。

現在,我正在通過Interface Builder創建UITableView,並將其插座連接到我的控制器類。

典型代碼:

@interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{ 
    IBOutlet UITableview *myTableview; 
} 

我想要做的事更是這樣的:

@interface MyController : UIViewController 
{ 
    IBOutlet UITableview *myTableview; 
} 
@end 

@interface MyTableSourceDelegate : NSObject<UITableViewDelegate, UITableViewDataSource> 
{ 
} 

@implementation MyTableSourceDelegate 
    // implement all of the UITableViewDelegate and methods in this class 
@end 
+0

可能重複[UITableView問題時使用單獨的委託/數據源](http://stackoverflow.com/questions/254354/uitableview-issue-when-using-separate-delegate-datasource) – 2011-02-03 08:23:12

回答

0

您可以創建separe類(用的UITableViewDelegate,UITableViewDataSource)和IB增加他們作爲外部文件鏈接IBActions

+0

我有單獨的類在MyController中實現UITableViewDelegate,UITableViewDataSource。你如何將這些實例鏈接爲IBActions? – 2010-01-19 23:14:31

0

在IB中,您可以從Library-> Cocoa Touch-> Controllers拖動'External Object'到xib窗口。然後,您可以選擇該對象,查看檢查員並設置班級。它現在可以作爲代表等

+0

IB是否負責此類的實例化?我需要做任何事情來使用它嗎?我試過這個,但是由於'EXC_BAD_ACCESS',我的應用似乎崩潰了。 – 2010-01-20 00:49:27

+0

是的,你說得對。我現在得到同樣的東西,我可以測試它。我通過拖動一個Object而不是External Object來重做這個。接下來,我在文件的所有者控制器中聲明一個IBOutlet類型爲Object項目的類型。然後,我將目標新控件的委託與新對象,新對象對新控件的視圖以及新IBOutlet鏈接到新對象,並且它會運行! – shawnwall 2010-01-20 01:44:52

+0

僅供參考IBOutlet的原因是新實例化後需要保留新控件。如果你不這樣做,它會立即被銷燬,本質上你得到一個空指針異常。 – shawnwall 2010-01-20 01:45:35

2

我花2小時內解決這個問題:

它的工作對我來說

// GenreDataSource.h 

#import Foundation/Foundation.h 

    @interface GenreDataSource : NSObject <UITableViewDataSource> { 
     NSArray *dataSource; 
     CGSize cellSize; 
    } 

@property(nonatomic, assign) CGSize cellSize; 

@end 



// GenreDataSource.m 
#import "GenreDataSource.h" 

@implementation GenreDataSource 
@synthesize cellSize; 

-(id)init{ 

    self = [super init]; 
    if (self != nil) { 

     dataSource = [[NSArray alloc] initWithObjects:@"All",@"Folk",@"Disco",@"Blues",@"Rock",@"Dance",@"Hip-Hop",@"R&B",@"Soul",@"Lounge",@"Techno",@"Bubstep", nil]; 
    } 
    return self; 
} 

#pragma mark - UITableViewDataSource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [dataSource count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"CellPicker"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 

     //сконфигурируем структуру 
     FontLabel *fLabel= [[FontLabel alloc] initWithFrame:CGRectMake(30, 
                     5, 
                     cellSize.width-30, 
                     cellSize.height-5) 
                fontName:@"HelveticaNeueCondensedBlack" 
                pointSize:18.0f]; 
     [fLabel setTextColor:[UIColor darkTextColor]]; 
     [fLabel setTag:101]; 
     [fLabel setBackgroundColor:[UIColor clearColor]]; 
     [cell.contentView addSubview:fLabel]; 
     [fLabel release]; 
    } 

    FontLabel *fLabel = (FontLabel*)[cell viewWithTag:101]; 
    [fLabel setText:[dataSource objectAtIndex:indexPath.row]]; 

    return cell; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

@end 
2

首先的事情是,如果你使用一個UITableViewController子類與界面生成器,你將要斷開代表和數據源插座默認已掛鉤。 (提示,看看連接檢查員)。檢查即使您在viewController中有一個tableView。

第二個創建您的課程,並確保它們符合<UITableViewDelegate><UITableViewDataSource>。如果您使用objc,您可能必須在.h文件中聲明此合約。

,在您的視圖控制器實例化這個類或兩個獨立的類象viewDidLoad某處,然後分配self.tableView.delegate = myCustomDelegateInstanceself.tableView.dataSource = myCustomDataSourceInstance

現在,通過控制器發出的任何呼叫都將被分派給您的自定義處理程序。很基本。

真正做到這一點的唯一原因是,如果你1)有一個非常臃腫的控制器,或2)你需要重用其他地方的dataSource和委託方法,並且你想避免代碼重複。否則,放棄它可能是更好的做法。

相關問題