可以在UIView中添加两个tableView吗?

时间:2011-03-15 06:58:53

标签: iphone objective-c uitableview uiviewcontroller

我在我的应用中使用了两个UItableView。我将他们的委托和数据源设置为文件的所有者。 在实施部分我尝试过:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView=tableView1) 
{
return [tableArr count];
}
if (tableView=tableView2) 
{
    return [tableArr count];
}

}

方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (tableView==tableView1) 
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        }

        cell.textLabel.text=[tableArr objectAtIndex:indexPath.row];
        return cell;

    }
    if (tableView==tableView2) 
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        }

        cell.textLabel.text=[tableArr objectAtIndex:indexPath.row];
        return cell;

    }

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

我也尝试过调用tableView,如tableView1& tableView2.But它显示空白表。那么可以在UIView中添加到tableView吗?

3 个答案:

答案 0 :(得分:3)

您实际上是在进行分配而不是比较(= vs ==),因此它始终会进行第一次返回。另外,请确保tableView1tableView2实际上是非零对象(IBOutlet连接或适当的init方法应该正常工作)。

答案 1 :(得分:1)

正如Eimantas所说,你应该尝试比较而不是分配,试试这个,并确保你也根据cellForRowAtIndexPath:

的需要检查表的内容。
  if (tableView==tableView1) 
{
return [tableArr count];
}
if (tableView==tableView2) 
{
    return [tableArr count];
}

答案 2 :(得分:0)

这是可能的,但我想

    if (tableView=tableView1) 
{
return [tableArr count];
}
if (tableView=tableView2) 
{
    return [tableArr count];
}

此代码假设在

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

而不是

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

如果您已从“界面”构建器添加它,还可以将引用IBoutlet设置为相应的表视图吗?

使用

[tableView isEqual:tableView1] / *而不是* / if(tableView = tableView1

相关问题