2013-02-20 58 views
1

在我的應用程序中,我想在單個ViewController中使用3個tableviews。問題是我怎樣才能單獨使用UITableViewDelegate方法。例如;我可以分別使用cellForRowAtIndexPath方法爲每個UITableView添加標籤。但是,我不知道如何針對每個tableview使用不同的numberOfRowsInSectionnumberOfSectionsInTableView方法。可能嗎?在視圖控制器中使用多個tableviews時使用委託方法

回答

4

請在YourViewController.h 3個UITableView變量:

YourViewController : UIViewController 
{ 
    UITableView* tableView1; 
    UITableView* tableView2; 
    UITableView* tableView3; 
} 

YourViewController.m:

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if (tableView == tableView1) 
    { 
     //Your code 
    } 
    if (tableView == tableView2) 
    { 
     //Your code 
    } 
    if (tableView == tableView3) 
    { 
     //Your code 
    } 
} 
+0

如是,感謝編輯! – 2013-02-20 13:07:07

2

您不應該使用單獨的委託方法。相反,像cellForRowAtIndexPath每個代理方法,你應該確定你的表作爲

if(tableview == TableView1) 
{ 

} 
else if(tableview == TableView2) 
{ 

} 
else 
{ 

} 

等。這是正確的方法,因爲您操作的表將具有通用的委託方法,然後您只需指定表的名稱。

1

當然:

所有的tableview委託函數具有的tableView作爲第一個參數,因此,所有你需要做的就是保持跟蹤的三點表意見,並在每個代表功能檢查該表中查看委託電話是for:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(tableView == firstTableView) { 
    ... 
    } 
    else if (tableView == secondTableView) { 
    ... 
    } 
    else if (tableView == thirdTableView) { 
    ... 
    } 
} 
0

在所有委託和數據源方法中,第一個參數是對tableview對象的引用。所以你總是可以區分你的桌面。

4

對數據源和委託方法中的所有帶有條件的表只使用一個dataSource和Delegate方法。

 - (NSInteger)numberOfRowsInSection:(NSInteger)section; 
     { 
    return 1; 
    } 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section; 
    { 
if (tableView==tabl1) { 
     return [arr1 count]; 
} 
if (tableView==tabl2) { 
     return [arr2 count]; 

} 
if (tableView==tabl3) { 
     return [arr3 count]; 
} 
return 0; 

    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell==nil) { 
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ]; 
    cell.selectionStyle=UITableViewCellSelectionStyleNone; 
    cell.backgroundColor=[UIColor clearColor]; 
} 
if (tableView==tabl1) { 
    cell.textLabel.text = [arr1 objectAtIndex:indexPath.row]; 
} 
if (tableView==tabl2) { 
    cell.textLabel.text = [arr2 objectAtIndex:indexPath.row]; 
} 
if (tableView==tabl3) { 
    cell.textLabel.text = [arr3 objectAtIndex:indexPath.row]; 
} 
return cell; 
     } 
0

ViewController.h

{ 
    NSArray *arr1; 
    NSArray *arr2; 
    NSArray *arr3; 
} 
@property (nonatomic, retain) IBOutlet UITableView *tbl1; 
@property (nonatomic, retain) IBOutlet UITableView *tbl2; 
@property (nonatomic, retain) IBOutlet UITableView *tbl3; 

不要忘記將此Properites與XIB's UITableView實例連接起來。

ViewController.m

@synthesize tbl1, tbl2, tbl3; 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if (tableView == tbl1) 
     return [arr1 count]; 
    if (tableView == tbl2) 
     return [arr2 count]; 
    if (tableView == tbl3) 
     return [arr3 count]; 
    return 0; 
} 

我希望我幫你。

0

您也可以創建三個表視圖控制器類(以防每個表視圖在其單元顯示邏輯中涉及一點複雜性)。將它們添加爲[self addChildViewController:(Your Controller Class),然後將下一行[self.view addSubview:(Your Controller Class' view)]與視圖調整爲您要設置的框架。