一个UIViewController里面添加两个UITableView 如何更新表格数据

Carl_1985 发布于 2015/09/19 14:29
阅读 2K+
收藏 0

大家好,目前遇到一个问题,在一个View里面添加了两个UITableView,点击cell的时候,下面插入一个新的cell,在新的cell里面有button,点击这个button的时候,我想刷新左右两个UITableView,大致情况如下:

1、我在viewDidLoad里面实例化了两个UITableView(_leftTableView和_rightTableView),并设置了代理为self,定义两个可变数组分别存储UITableView数据

2、实现了UITableView代理的基本方法,各个方法返回值确认没有问题,且两个表格显示的数据符合预期

3、点击button,想更新表格数据的时候,分别调用[_leftTableView reloadData];[_rightTableView reloadData];这个时候表格没有更新数据,不知为甚么,求大神指点下,谢谢!

5、显示界面如下:(点击右边Cell里面的进入,想要刷新左右两个表格的数据)


4、补充一下,点击button的时候,我调用的函数如下:

- (IBAction)doGoToSubPage:(id)sender {

    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];

    UITableView *tableView = (UITableView *)[[cell superview] superview];

    NSIndexPath *indexPath = [tableView indexPathForCell:cell];    

    NSLog(@"tag:%ld",tableView.tag);

    if (100 == tableView.tag)

    {

        [self requestChildData];

        [self.rightTableView reloadData];

    }

    else

    {

        NSLog(@"indexPath:section:%ld row:%ld",indexPath.section,indexPath.row);

        [self requestChildData];//更新可辨数组中的数据

        NSLog(@"Table:%@",tableView);

        NSLog(@"Table_L:%@",_leftTableView);

        NSLog(@"Table_R:%@",_rightTableView);

        //        [_leftTableView reloadData];

        [_rightTableView reloadData];//UITableView没有刷新,求指点

        [_leftTableView reloadData];//UITableView没有刷新,求指点

 //       [_rightTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

               [tableView reloadData];//右边的UITableView会刷新

    }

}



5、完整代码如下:

===============================ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) NSMutableArray *leftArray;

@property (strong,nonatomic) NSMutableArray *rightArray;

@property (strong, nonatomic) UIView *leftView;

@property (strong, nonatomic) UIView *rightView;

@property (strong, nonatomic) UITableView *leftTableView;

@property (strong, nonatomic) UITableView *rightTableView;

@property (retain, nonatomic) UILabel *leftPathLabel;

@property (retain, nonatomic) UILabel *rightPathLabel;

//- (IBAction)doCloseWork:(id)sender;

- (IBAction)doGoToSubPage:(id)sender;

- (IBAction)doAddFCWork:(id)sender;

- (IBAction)doCallOutWork:(id)sender;

@end




===============================ViewController.m

#import "ViewController.h"


#define KEY_DEVINFO_ID              @"DevID"
#define KEY_DEVINFO_TYPE            @"DevType"
#define KEY_DEVINFO_NAME            @"DevName"
#define KEY_DEVINFO_ADDRESS         @"DevAddress"
#define KEY_DEVINFO_REMARK          @"Remark"
#define KEY_DEVINFO_CODE            @"Code"
#define KEY_DEVINFO_CODE_PARENT     @"ParentCode"
#define KEY_DEVINFO_CHILD_CNT       @"ChildCnt"

#define KEY_DEVINFO_CELL            @"Cell"
#define KEY_CELL_STATE              @"State"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.frame = CGRectMake(76, 0,476*2, 768);
    self.view.backgroundColor = [UIColor clearColor];
    
    [self addressBookDataInit];
    
    //left
    _leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,476, 768)];
    _leftView.backgroundColor = [UIColor colorWithRed:214.0/255 green:214.0/255 blue:214.0/255 alpha:1.0];
    _leftPathLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0,240, 75)];
    UIButton *leftCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftCloseButton.frame = CGRectMake(400, 0,76, 75);
    [leftCloseButton setTitle:@"关闭" forState:UIControlStateNormal];
    [leftCloseButton addTarget:self action:@selector(doCloseWork:) forControlEvents:UIControlEventTouchUpInside];
    _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 75,476, 693) style:UITableViewStylePlain];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    _leftTableView.tag = 100;
    
    NSLog(@"line:%d",__LINE__);
    [_leftView addSubview:_leftPathLabel];
    [_leftView addSubview:leftCloseButton];
    [_leftView addSubview:_leftTableView];
    NSLog(@"line:%d",__LINE__);
    //right
    _rightView = [[UIView alloc] initWithFrame:CGRectMake(476, 0,476, 768)];
    _rightView.backgroundColor = [UIColor colorWithRed:214.0/255 green:214.0/255 blue:214.0/255 alpha:1.0];
    _rightPathLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0,240, 75)];
    UIButton *rightCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightCloseButton.frame = CGRectMake(400, 0,76, 75);
    
    [rightCloseButton setTitle:@"关闭" forState:UIControlStateNormal];
    [rightCloseButton addTarget:self action:@selector(doCloseWork:) forControlEvents:UIControlEventTouchUpInside];
    _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 75,476, 693)];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
    _rightTableView.tag = 200;
    
    NSLog(@"line:%d",__LINE__);
    [_rightView addSubview:_rightPathLabel];
    [_rightView addSubview:rightCloseButton];
    [_rightView addSubview:_rightTableView];
    
    self.leftTableView.separatorStyle = NO;
    self.rightTableView.separatorStyle = NO;
    
    [self.view addSubview:_leftView];
    [self.view addSubview:_rightView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addressBookDataInit
{
    _leftArray = [[NSMutableArray alloc] initWithCapacity:20];
    _rightArray = [[NSMutableArray alloc] initWithCapacity:20];
    
    NSMutableDictionary *dev1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",KEY_DEVINFO_CELL,@"1",KEY_DEVINFO_TYPE,@"0",KEY_DEVINFO_ID,@"上海Insight_001",KEY_DEVINFO_NAME,@"Shanhai",KEY_DEVINFO_ADDRESS,@"001",KEY_DEVINFO_CODE,@"",KEY_DEVINFO_CODE_PARENT,@"1",KEY_DEVINFO_CHILD_CNT,@"YES",KEY_CELL_STATE,nil];
    NSMutableDictionary *dev2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",KEY_DEVINFO_CELL,@"1",KEY_DEVINFO_TYPE,@"0",KEY_DEVINFO_ID,@"Insight_001",KEY_DEVINFO_NAME,@"陆家嘴",KEY_DEVINFO_ADDRESS,@"002",KEY_DEVINFO_CODE,@"",KEY_DEVINFO_CODE_PARENT,@"0",KEY_DEVINFO_CHILD_CNT,@"NO",KEY_CELL_STATE,nil];
    NSMutableDictionary *dev3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",KEY_DEVINFO_CELL,@"1",KEY_DEVINFO_TYPE,@"1",KEY_DEVINFO_ID,@"上海001",KEY_DEVINFO_NAME,@"Shanhai",KEY_DEVINFO_ADDRESS,@"003",KEY_DEVINFO_CODE,@"",KEY_DEVINFO_CODE_PARENT,@"0",KEY_DEVINFO_CHILD_CNT,@"NO",KEY_CELL_STATE,nil];
    NSMutableDictionary *dev4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",KEY_DEVINFO_CELL,@"1",KEY_DEVINFO_TYPE,@"2",KEY_DEVINFO_ID,@"上海Insight_002",KEY_DEVINFO_NAME,@"Shanhai",KEY_DEVINFO_ADDRESS,@"001",KEY_DEVINFO_CODE,@"",KEY_DEVINFO_CODE_PARENT,@"0",KEY_DEVINFO_CHILD_CNT,@"NO",KEY_CELL_STATE,nil];
    
    [_leftArray addObject:dev1];
    [_leftArray addObject:dev2];
    [_leftArray addObject:dev3];
    [_leftArray addObject:dev4];
    
    NSMutableDictionary *dev5 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",KEY_DEVINFO_CELL,@"2",KEY_DEVINFO_TYPE,@"1",KEY_DEVINFO_ID,@"华丽家族",KEY_DEVINFO_NAME,@"南京",KEY_DEVINFO_ADDRESS,@"001",KEY_DEVINFO_CODE,@"",KEY_DEVINFO_CODE_PARENT,@"1",KEY_DEVINFO_CHILD_CNT,@"NO",KEY_CELL_STATE,nil];
    [_rightArray addObject:dev5];
}



- (void)requestChildData
{
    NSMutableArray *array_L = [[NSMutableArray alloc] initWithCapacity:10];
    NSMutableArray *array_R = [[NSMutableArray alloc] initWithCapacity:10];
    [array_L addObjectsFromArray:_leftArray];
    [array_R addObjectsFromArray:_rightArray];
    
    [_rightArray removeAllObjects];
    [_leftArray removeAllObjects];
    [_leftArray addObjectsFromArray:array_R];
    [_rightArray addObjectsFromArray:array_L];
  
}

- (IBAction)doGoToSubPage:(id)sender {
    NSLog(@"[AddressBook]   go to sub page.");
    
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
    UITableView *tableView = (UITableView *)[[cell superview] superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];//nil;//[_addressTableView indexPathForCell:cell];
    
    NSLog(@"tag:%ld",tableView.tag);
    
    if (100 == tableView.tag)
    {
//        [self requestChildData:_leftArray withRow:indexPath.row - 1];
        [self requestChildData];
        [self.rightTableView reloadData];
    }
    else
    {
        NSLog(@"indexPath:section:%ld row:%ld",indexPath.section,indexPath.row);
        [self requestChildData];
        NSLog(@"Table:%@",tableView);
        NSLog(@"Table_L:%@",_leftTableView);
        NSLog(@"Table_R:%@",_rightTableView);
        //        [_leftTableView reloadData];
        [_rightTableView reloadData];
        [_leftTableView reloadData];
 //       [_rightTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
               [tableView reloadData];
    }
}


#pragma mark----tableViewDelegate
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    if ([tableView isEqual:_leftTableView])
    if (100 == tableView.tag)
    {
        //       _leftTableView = tableView;
        return _leftArray.count;
    }
    else
    {
        //        _rightTableView = tableView;
        return _rightArray.count;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d",__LINE__);
    NSMutableArray *dev = [[NSMutableArray alloc] initWithCapacity:10];
    //    if ([tableView isEqual:_leftTableView])
    
    if (100 == tableView.tag)
    {
        [dev addObjectsFromArray:_leftArray];
    }
    else
    {
        [dev addObjectsFromArray:_rightArray];
    }
    
    NSString *cellType = [[dev objectAtIndex:indexPath.row] objectForKey:KEY_DEVINFO_CELL];
    if ([cellType isEqualToString:@"MainCell"])
    {
        return 76.0;
    }
    else
    {
        return 58.0;
    }
    
    return 76;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AddressBook";
    NSMutableArray *devArray = [[NSMutableArray alloc] initWithCapacity:10];
    [devArray removeAllObjects];
    
    //    if ([tableView isEqual:_leftTableView])
    if (100 == tableView.tag)
    {
        [devArray addObjectsFromArray:_leftArray];
    }
    else
    {
        [devArray addObjectsFromArray:_rightArray];
    }
    NSLog(@"indexPath.row:%ld",indexPath.row);
    
    NSString *cellType = [[devArray objectAtIndex:indexPath.row] objectForKey:KEY_DEVINFO_CELL];
    
    if ([cellType isEqualToString:@"MainCell"])
    {
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        NSLog(@"%d",__LINE__);
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        NSString *name = [[devArray objectAtIndex:indexPath.row] objectForKey:KEY_DEVINFO_NAME];
        NSString *address = [[devArray objectAtIndex:indexPath.row] objectForKey:KEY_DEVINFO_ADDRESS];
        cell.imageView.image = [UIImage imageNamed:@"intercom_group"];
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"intercom_star"]];
        
        if (![name isEqual:@""])
        {
            cell.textLabel.text = name;
        }
        else
        {
            cell.textLabel.text = address;
        }
        
        return cell;
    }
    else
    {
        NSLog(@"[%@]_[%ld %ld]%d",cellType,indexPath.section,indexPath.row,__LINE__);
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (nil == cell)
        {
            NSUInteger childCnt = [[[devArray objectAtIndex:indexPath.row - 1] objectForKey:KEY_DEVINFO_CHILD_CNT] integerValue];
            NSUInteger devType = [[[devArray objectAtIndex:indexPath.row - 1] objectForKey:KEY_DEVINFO_TYPE] integerValue];
            
            if (childCnt)
            {
                if (1 == devType)
                {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"AddressBookSubView" owner:self options:nil] objectAtIndex:4];
                }
                else
                {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"AddressBookSubView" owner:self options:nil] objectAtIndex:2];
                }
            }
            else
            {
                if (1 == devType)
                {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"AddressBookSubView" owner:self options:nil] objectAtIndex:3];
                }
                else
                {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"AddressBookSubView" owner:self options:nil] objectAtIndex:5];
                }
            }
        }
        
        //        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSLog(@"%d",__LINE__);
        //        cell = [nibViewArray objectAtIndex:3];
        return cell;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    [dataArray removeAllObjects];
    NSIndexPath *path = nil;
    //    if ([tableView isEqual:_leftTableView])
    if (100 == tableView.tag)
    {
        [dataArray addObjectsFromArray:_leftArray];
    }
    else
    {
        [dataArray addObjectsFromArray:_rightArray];
    }
    
    NSString *cellType = [[dataArray objectAtIndex:indexPath.row] objectForKey:KEY_DEVINFO_CELL];
    NSUInteger devType = [[[dataArray objectAtIndex:indexPath.row] objectForKey:KEY_DEVINFO_TYPE] intValue];
    NSString *cellState = [[dataArray objectAtIndex:indexPath.row] objectForKey:KEY_CELL_STATE];
    NSMutableDictionary *dd = [dataArray objectAtIndex:indexPath.row];
    
    
    NSLog(@"[Section:%ld    Row:%ld]--->indexPath",indexPath.section,indexPath.row);
    //    [self tableView:tableView closeSubCellAtIndexPath:_closePath];
    
    if ([cellType isEqualToString:@"MainCell"]) {
        path = [NSIndexPath indexPathForItem:(indexPath.row+1) inSection:indexPath.section];
    }
    else
    {
        NSLog(@"line:%d",__LINE__);
        path = indexPath;
        return;
    }
    
    
    
    // 打开附加cell
    if ([cellState boolValue])
    {
        NSLog(@"===============================Close.");
        NSLog(@"row:%ld",indexPath.row);
        //      NSMutableDictionary *dd = _dataDic[indexStr][indexPath.row];
        
        // 关闭附加cell
        NSMutableDictionary *dicSrc = [[NSMutableDictionary alloc] initWithDictionary:dd];
        [dicSrc setObject:@"NO" forKey:KEY_CELL_STATE];
        
        dataArray[(path.row - 1)] = dicSrc;
        [dataArray removeObjectAtIndex:path.row];
        
        //        if ([tableView isEqual:_leftTableView])
        if (100 == tableView.tag)
        {
            [_leftArray removeAllObjects];
            [_leftArray addObjectsFromArray:dataArray];
        }
        else
        {
            [_rightArray removeAllObjects];
            [_rightArray addObjectsFromArray:dataArray];
        }
        
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[path]  withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView endUpdates];
        
        return;
    }
    
    NSLog(@"===============================OPEN.");
    //   NSMutableDictionary *dd = _dataDic[indexStr][indexPath.row];
    
    NSMutableDictionary *dicSrc = [[NSMutableDictionary alloc] initWithDictionary:dd];
    [dicSrc setObject:@"YES" forKey:KEY_CELL_STATE];
    
    dataArray[(path.row - 1)] = dicSrc;
    NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"SubCell",KEY_DEVINFO_CELL,@"YES",KEY_CELL_STATE,nil];
    [dataArray insertObject:nameAndStateDic1 atIndex:path.row];
    
    //    if ([tableView isEqual:_leftTableView])
    if (100 == tableView.tag)
    {
        [_leftArray removeAllObjects];
        [_leftArray addObjectsFromArray:dataArray];
    }
    else
    {
        [_rightArray removeAllObjects];
        [_rightArray addObjectsFromArray:dataArray];
    }
    
    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
    [tableView endUpdates];
}


@end





加载中
0
C
Carl_1985

补充一下:

[_rightTableView reloadData];//TableView没有更新

[_leftTableView reloadData];//TableView没有更新

[(UITableView *)[self.view viewWithTag:200] reloadData];//TableView成功更新

[(UITableView *)[self.view viewWithTag:100] reloadData];//TableView成功更新

在我想用UITableView对象的时候,用_rightTableView都没有起效,不知为什么?
返回顶部
顶部