2013-11-26 53 views
2

我如何實現以下功能。視圖上的兩個UITableView

我有一個觀點,就是我想要添加兩個UITableView爲查看的子視圖..

  1. monthTableView
  2. dayTableView

both tableView frame size是父視圖大小。

但一次只能看到一個表格。 例如:

我需要一個方法,所以當我調用該方法需要檢查哪個表視圖通過標誌變量當前可見。

if (visibleTable == monthTableView) 
{ 

    //need to add subview of parent view is dayTableView 
    [self addSubview:dayTableView]; 
} 

if (visibleTable == dayTableView) 
{ 

    //need to add subview of parent view is monthTableView. 
    [self addSubview:monthTableView]; 
} 

我該如何做到這一點?請親引導我。在此先感謝...

+0

@ user28882259參考我的答案的情況。肯定會幫助你。 – Smita

回答

2

通過給tag.i.e區分這兩個表。

對於第一個表視圖 -

firstTableView.tag = 1; 

對於第二個表視圖 -

secondTableView.tag = 2; 

,並在數據源和委託方法按標籤區分表視圖 -

if(tableView.tag == 1){ 

    //do something 

}else{ 

    //do something for send table view 

} 
2

您需要設置兩個表格視圖的「標籤」值,然後使用以下代碼檢查標籤值:

if (tableView.tag == 0) { 
    // tableView 1 
} 
else if (tableView.tag == 1) { 
    // tableView 2 
} 

您也可以在界面構建器或代碼中設置標記值。使用setTag:方法。

3

在表格視圖中的委託方法中,您將看到一個參數tableView,它只是您當前的tableView。你可以使用它來區分你的tableViews。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(tableView == monthTableView) 
    { 
     //code for monthTableView 
    } 
    else if(tableView == dayTableView) 
    { 
     //code for dayTableView 
    } 
    //extra coding 
} 
2

您可以通過使用單個tableview來實現此目的。你需要做的是使用數據源和委託方法來區分Day和Month的數據。你能做什麼是使用枚舉表數據一鍵是日,第二是月。 現在在控制器的.h中

enum TableContentType { 
    kDay = 0, 
    kMonth = 1 
}; 


@property (assign) enum TableContentType tableContentType; 
@property (nonatomic, strong) NSMutableArray * monthDataArray; 
@property (nonatomic, strong) NSMutableArray * dayDataArray; 

現在在。M檔

的動作,從日表的內容切換到月或反之亦然

- (IBAction)changeTableContent:(id)sender 
{ 
    self.tableContentType = (self.tableContentType == kDay) ? kMonth : kDay; 

    [self.table reloadData]; 

} 

在表的數據源和委託方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return (self.tableContentType == kDay) ? self.dayDataArray.count : self.monthDataArray.count ; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 



    if (cell == nil) { 
     cell = (self.tableContentType == kDay) ? [self createDayCellForIndexPath: indexPath] : [self createMOnthCellForIndexPath: indexPath]; 

    } 

return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(self.tableContentType == kDay) 
    { 
     [self dayCellTappedForIndexPath: indexPath]; 
    }else 
    { 
     [self monthCellTappedForIndexPath: indexPath]; 
    } 

} 

方法爲白天鑑於indexpath創建細胞和月

- (UITableViewCell *)createDayCellForIndexPath:(NSIndexPath*)indexPath 
{ 
    UITableViewCell * dayCell = nil; 
    // write your code for day cell here 


    return dayCell; 
} 

- (UITableViewCell *)createMonthCellForIndexPath:(NSIndexPath*)indexPath 
{ 
    UITableViewCell * monthCell = nil; 
    // write your code for month cell here 


    return monthCell; 
} 

執行單元格選擇操作的方法日,月

- (void)dayCellTappedForIndexPath: (NSIndexPath *)indexPath 
{ 

    // write your code for day cell tap 

} 

- (void)monthCellTappedForIndexPath: (NSIndexPath *)indexPath 
{ 

    // write your code for month cell tap 

} 

這將有助於你奶源您有