2011-12-25 132 views
0

我正在尝试将2 UITableView添加到我的UIViewController。我需要为这些表添加不同的数据。将数据添加到2个表

这是我加入2个表(这个代码加入到viewDidLoad方法)

self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0,140,292,250) style:UITableViewStylePlain] ; 
self.tableView2 .dataSource = self; 
self.tableView2 .delegate = self; 

那么其他表

self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,100) style:UITableViewStylePlain] ; 
self.tableView1 .dataSource = self; 
self.tableView1 .delegate = self; 

定义如下部分的数量;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView==tableView1) { 
     return 12; 
    } 
    else { return 10; } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // ...... more code here 
    if (tableView == self.tableView1) {  
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];   
     } 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [email protected]"Cells .... "; 
    } 
    else{ 
     // the remaining code here.. i am populating the cell as in the previous `IF` condition. 
    } 
} 

问题是,我只得到第一个表填充,而不是第二个表。为什么是这样?我该如何解决这个问题?

编辑: 我还添加以下代码,希望作出改变

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

您是如何的tableViews添加到上海华? – vikingosegundo 2011-12-25 15:07:20

+0

呃..我根据上面的代码做了。并显示出来。我无法填充它。需要帮助! – Illep 2011-12-25 15:08:54

+0

肯定可以确定你在else部分返回任何东西吗? – 2011-12-25 15:18:21

回答

3

尽量遵循为了使具有相同delegatedataSource两个表视图这些步骤。

  1. 设置你的表视图,并在这两个值#define常数tag财产。这使得代码一致。

  2. 在您在视图控制器子类中实现的委托和数据源方法中,根据您定义的常量测试tag属性值。

  3. 不要为表视图返回0节,它根本不会显示任何单元格。

因此,举例来说:

#define TV_ONE 1 
#define TV_TW0 2 

// setting the tag property 
self.tableView1 = [[UITableView alloc] 
        initWithFrame:CGRectMake(0,0,320,100) 
          style:UITableViewStylePlain]; 
self.tableView1.tag = TV_ONE; 
self.tableView1.dataSource = self; 
self.tableView1.delegate = self; 
// the same for tableView2 using TV_TWO 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView.tag == TV_ONE) { 
     return 1; 
    } 
    else if (tableView.tag == TV_TWO) { 
     return 1; // at least one section 
    } 
    else { return 0; } 
}