2012-11-18 19 views
4

所以我想在一個視圖中做兩個tableviews,我遇到了一些麻煩。 我已經閱讀了一些關於如何做的反應,但他們並不完全幫助我。在一個控制器中的兩個Tableviews

在我.h文件中我做了兩個網點爲兩種觀點稱他們在我的M檔myFirstViewTextmySecondViewTex

所以對 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

我希望能夠在各個不同的打印出單獨的值控制器,我不太確定,因爲你只返回1個單元格?

到目前爲止我做這個

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Rx"; 
static NSString *CellIdentifier2 = @"allergies"; 
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier]; 
UITableViewCell *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier2]; 
if(!cell){ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
if (!cell2) { 
    cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; 
} 
if (tableView == self.myFirstTextView) { 
     cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row]; 
} 
if (tableView == self.mySecondTextView) { 
     cell.textLabel.text = @"BYE JAZZY";//[RxDict objectAtIndex:indexPath.row]; 
} 
tableView = self.mySecondTextView; 
cell2.textLabel.text = @"I love Jazzy :D"; 
return cell2; 

這將打印「我愛爵士」在我第一次TableView中沒有東西印在了第二個。爲什麼會發生這種情況,我該如何解決? 感謝:D

+0

爲什麼這個問題被低估? –

回答

7

此方法被所有tableViews調用,它們的類的實例設置爲它們的dataSource。

這意味着您需要檢查哪個 tableView要求的細胞數量如此之多。

所以,你的方法應該基本上是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == rxTableView) { 
     //prepare and return the appropriate cell for *this* tableView 
    } 
    else if (tableView == allergiesTableView) { 
     //prepare and return the appropriate cell for *this* tableView 
    } 
    return nil; //because you don't expect any other tableView to call this method 
} 
+0

請注意,返回nil會導致程序崩潰......您最好返回空白單元格或提醒用戶發生不可恢復的錯誤,並讓他們重新啓動應用程序。 – lnafziger

+1

@lnafziger你說得對,這會導致應用崩潰。我認爲這是一件好事。您不希望任何您不知道的TableView需要數據,而且您也無法提供它。如果某些事情無法像這裏的情況那樣進行靜態檢查,儘可能早地崩潰是很好的。無論如何,這不在話下 - 返回那裏並不重要,因爲這條線永遠不會到達。 – fzwo

+0

其實,在我看來,當你可以提供受控關閉時,崩潰是不好的。但是值得指出的是那些不知道何時將其包含在代碼中的人。 :) – lnafziger

1

一種方法我沒有看到實施往往實際上是使用NSObject的子類來充當委託和數據源中的每個表,不視圖控制器。它消除了對「if(tableView == someTable)...」代碼的需求,並且可能使您的視圖控制器和UITableView代碼可維護且可讀。

你會爲每個表創建一個NSObject子類。這些子類將包含您的UITableView數據源和委託方法的所需實現。然後你會在你的視圖控制器中實例化每一個,並將每個鉤到適當的表。

4

我的建議是在你的兩個表視圖上設置一個標記,然後在你的tableview數據源方法中檢查你的表視圖的標記。例如,在你的代碼中你可以執行的某些地方:

myFirstTableView.tag = 1; 
mySecondTableView.tag = 2; 

後,當你在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView.tag == 1) 
    cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row]; 

    if (tableView.tag == 2) 
    cell.textLabel.text = @"BYE JAZZY"; 
} 

此外,如果你有2個tableviews大小不同,你可以這樣實現這一點:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    if (tableView.tag == 1) 
     return 1; 
    else if (tableView.tag == 2) 
     return 2; 
    } 
1

問題1:

tableView = self.mySecondTextView;  
cell2.textLabel.text = @"I love Jazzy :D"; 
return cell2; 

你在做什麼在這裏總是返回cell2,設置它的文字「我愛爵士:d」,因此,它會永遠填補第一個表視圖,因爲你不會設置tableView=mySecondTextView在進入該方法之前。代表總是以firstTableView爲目標。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

問題2:

if (tableView == self.myFirstTextView) { 
    cell.textLabel.text = @"HI JAZZY"; 
    //[RxDict objectAtIndex:indexPath.row]; 

    //for printing in first table view return cell here like 

    return cell; 
} 
if (tableView == self.mySecondTextView) { 
    cell.textLabel.text = @"BYE JAZZY"; 
    //[RxDict objectAtIndex:indexPath.row]; 

    //for printing in second tableview return cell two here 

    return cell2; 
} 

確保您要定位的表格進入此功能之前設置。您可以將它設置爲numberofsection方法,查看didload方法或從您想要顯示的位置或目標表視圖的其他位置(如單擊某個按鈕)。但是你不能在這裏設置它。

相關問題