两个tableview 在同一个页面时,出现自动合并的现象

问题:两个tableview  在同一个页面时,出现自动合并的现象

问题代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *cellIdetify = @"cell";
        UITableViewCell *tvCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetify] autorelease];  
        tvCell.selectionStyle = UITableViewCellSelectionStyleGray;
        tvCell.selectionStyle = NO;    //让单元格无法被选中
    
    if (tableView.tag == 1000) {       
        [tvCell addSubview:[arrViews objectAtIndex:indexPath.row]];
        return tvCell;
    }else if (tableView.tag == 2000) {
        [tvCell2 addSubview:[arrViews2 objectAtIndex:indexPath.row]];
        return tvCell2;
    }else {
        return 0;
    }
}

原因分析:

通过仔细阅读上述代码,我们可以发现,这两个tableview 用的是同一种类型的cell,所以,建议不同的tableview使用不同的cell

建议修改代码如下:

这样,我们就可以对两个不同的tableview 分比进行编辑和配置了。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if (tableView.tag == 1000) {
        static NSString *cellIdetify = @"cell";
        UITableViewCell *tvCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetify] autorelease];  
        tvCell.selectionStyle = UITableViewCellSelectionStyleGray;
        tvCell.selectionStyle = NO;    //让单元格无法被选中
        
        [tvCell addSubview:[arrViews objectAtIndex:indexPath.row]];
        return tvCell;
    }else if (tableView.tag == 2000) {
        static NSString *cellIdetify2 = @"cell2";
        UITableViewCell *tvCell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetify2] autorelease];  
        tvCell2.selectionStyle = UITableViewCellSelectionStyleGray;
        tvCell2.selectionStyle = NO;    //让单元格无法被选中
        
        [tvCell2 addSubview:[arrViews2 objectAtIndex:indexPath.row]];
        return tvCell2;
    }else {
        return 0;
    }
}


发现:当一个页面有两个tableview时,如果这两个tableview 都没有设置边框的话,那么这两个tableview会出现合并的现象。只要为这两个tableview设置边框。和合适的间距。它们就会分开。 如何为tableview设置边框,详见:
http://www.cnblogs.com/ygm900/archive/2013/05/19/3087187.html

原文地址:https://www.cnblogs.com/ygm900/p/3087173.html