如何重新加载一个表视图行选择然后另一个tableview重新加载?

时间:2016-01-13 06:10:34

标签: ios objective-c uitableview

以下是我的示例代码。如何重新加载tblview(tableview)?请帮我。

@interface NewUIViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UITableView *tblview;
- (IBAction)btnAddRow:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tblviewProject;
@property (nonatomic, strong) NSMutableArray *projectArray;
@end
@interface NewUIViewController ()
    {   }
@end
@implementation NewUIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"Tiger",@"Leopard",@"Snow Leopard",@"Lion",nil];
    self.projectArray = [[NSMutableArray alloc] initWithObjects:@"test1",@"test12",@"test1 Leopard",@"test13",nil];];     
    self.tblview.delegate= self;
    self.tblview.dataSource=self;   
    self.tblviewProject.delegate= self;
    self.tblviewProject.dataSource =self;
    self.tblviewProject.frame = CGRectMake(50, 150, 600, 600);
}
-(BOOL)shouldAutorotate
{
    return  YES;
}
- (NSUInteger ) supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskLandscape);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tblview)
    {
    return [self.dataArray count];
    }
    else if (tableView == self.tblviewProject)
    {
        return [self.projectArray count];
    }
    return 0;
}
- (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];
        cell.editingAccessoryType = YES;
        if (tableView == self.tblview)
        {
            cell.textLabel.text =  [self.dataArray objectAtIndex:indexPath.row];
         }
        else if (tableView == self.tblviewProject)
        {
            cell.textLabel.text = [self.projectArray  objectAtIndex:indexPath.row]; 
        }        
    }   
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if(tableView == self.tblviewProject)
    {     
        NSMutableDictionary *newDic = [[NSMutableDictionary alloc] init];
        NSDictionary *rowchange = (NSDictionary *) [self.dataArray objectAtIndex:[self.projIndexPath intValue]];
        [newDic addEntriesFromDictionary:rowchange];
        [newDic setObject:pname forKey:project];       
        [self.tblview beginUpdates];
        [self.dataArray replaceObjectAtIndex:[self.projIndexPath intValue] withObject:newDic];         
        [self.tblview reloadData];
        [self.tblview endUpdates];   
    }
}
@end    

enter image description here

1 个答案:

答案 0 :(得分:-1)

你在didSelectRowAtIndexPath func中,projIndexPathreplaceObjectAtIndex是什么 而self.dataArray是NSString数组,但你Cellforrowatindexpath有一个字典,它是不正确的 我不知道你想要什么?

更新:你正在运行,但并不意味着它是正确的  在cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row]; 中设置了

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.tblviewProject)
    {
        [self.dataArray replaceObjectAtIndex:indexPath.row withObject:self.projectArray[indexPath.row]];
        [self.tblview reloadData];
    }
}

但是你将self.dataArray中的项目替换为字典==&gt;你认为,它有效吗? 尝试:

- (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];
        cell.editingAccessoryType = YES;
    }
    if (tableView == self.tblview)
    {
        cell.textLabel.text =  [self.dataArray objectAtIndex:indexPath.row];
    } else if (tableView == self.tblviewProject){
        cell.textLabel.text = [self.projectArray  objectAtIndex:indexPath.row];
    }
    return cell;
}

并根据需要替换pnam和projIndexPath

AXXXXXXXXXXX 更新cellforrowat

database

我确定这有效,当你为tableview重新加载数据时,单元格不是nil,所以,它不是为单元格设置数据