簡體   English   中英

如何在iphone的單視圖控制器中使用兩個以上的UITableView

[英]how to use more than two UITableView in single view controller in iphone

我在UIViewController使用了兩個UITableView ,如何在兩個tableviews中填充行和單元格? 當我給第二個tableview時,它表示部分中行數的重復聲明等。

這就是dataSource / delegate方法有一個tableView參數的原因。 根據其值,您可以返回不同的數字/單元格/ ...

- (void)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _myTableViewOutlet1)
        return 10;
    else
        return 20;
}

您的所有UITableViewDelegateUITableViewDatasource方法只能實現一次。 您只需要檢查調用該方法的表視圖。

if (tableView == tblView1) {
    //Implementation for first tableView
}
else {
    //Implementation for second tableView
}

這將適用於所有TableView的委托和數據源方法,因為tableView是所有方法中的通用參數

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

這里這里

此鏈接還有您的問題的解決方案。

希望這可以幫助

請看一看。

首先在界面構建器中創建兩個tableview,然后連接兩個IBOutlet變量,並為兩個tableviews設置委托和數據源。

在接口文件中

 -IBOutlet UITableView *tableView1;
 -IBOutlet UITableView *tableView2;

在實施文件中

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
  {
       if (tableView==tableView1)
       {
        return 1;
       }
       else 
       {
         return 2;
       }
  }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 {
    if (tableView==tableView1)
    {
         return 3;
    }
    else
    {
         if (section==0)
          {
            return 2;
          }
          else
          {
            return 3;
          }
     }
  }

 - (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];
            }

    if (tableView==tableView1)
         {
           //cell for first table
         }
    else 
         {
           //cell for second table
          }
    return cell;
 }

使用此代碼。 希望有所幫助

有可能,請看這里的參考代碼: http//github.com/vikingosegundo/my-programming-examples

另請參閱此頁面: 單個視圖上的2個tableview

暫無
暫無

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

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