在同一屏幕的tableview中重新加载数据

时间:2015-12-31 18:19:31

标签: ios uitableview

如何在favoriteTable单元格中删除联系人后重新加载数据(当我已经在favoriteTab中时,我删除联系人单击单元格中的删除按钮,我应该重新加载我已经在观看的同一屏幕)

favoritesTableViewController

//
//  favoritesTableViewController.m
//  MaverickApp
//
//  Created by Gregory Mezentsev on 12/30/15.
//  Copyright © 2015 Alex&Gregory. All rights reserved.
//
#import "ModelUser.h"
#import "favoritesTableViewCell.h"
#import "screenController.h"
#import "favoritesTableViewController.h"
#import "userDetailsProfile.h"

@interface favoritesTableViewController (){
    NSArray* myFavListId;
    NSMutableArray* myFavListContactsData;
}

@end

@implementation favoritesTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.actualLoggedUser  = [NSString stringWithFormat:@"2"];
    [self reloadData];
  }

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

- (void)reloadData {

    NSLog(@"Favorites tab was loaded");

    //get id of my favorite contacts
    myFavListId = [[ModelUser instance] getUser:self.actualLoggedUser].contactsFavoriteList;

    //get data of my favorites contacts
    myFavListContactsData = [[NSMutableArray alloc] init];
    for (int i=0; i < [myFavListId count] ; i++) {
        User* us = [[ModelUser instance] getUser:([myFavListId objectAtIndex:i])];
        [myFavListContactsData addObject:us];
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (void) viewDidAppear:(BOOL)animated {
    [self reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return myFavListContactsData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    favoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"favoriteCell" forIndexPath:indexPath];

    User *us = [myFavListContactsData objectAtIndex:indexPath.row];

    cell.actualLoggedUser = self.actualLoggedUser;
    cell.contactUserId = us.userId;
    cell.contactName.text = [NSString stringWithFormat:@"%@ %@",us.fname,us.lname];
    [cell.contactImage setImage: [UIImage imageNamed:us.imageName]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    User *us = [myFavListContactsData objectAtIndex:indexPath.row];

    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    userDetailsProfile*  udVC = [sb
                                 instantiateViewControllerWithIdentifier:@"userDetailsProfile"];
    udVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    udVC.userDetailId = [NSString stringWithFormat:@"%@", us.userId];
    [self showViewController:udVC sender:self];

}

@end

favoritesTableViewCell

#import "favoritesTableViewCell.h"
#import "ModelUser.h"

    @implementation favoritesTableViewCell

    - (void)awakeFromNib {
        // Initialization code
    }

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];

        // Configure the view for the selected state
    }

    - (IBAction)contactDelete:(id)sender{
        [[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
    }

    @end

1 个答案:

答案 0 :(得分:0)

您没有解释此问题,但在我看来,您应该从myFavListContactsData数组数据源中删除已删除的联系人,然后您可以重新加载。

代表适合这种情况。

您不必重新加载tableView,一旦从数据源中删除,您就可以从tableView删除单元格:

[tableView deleteRowsAtIndexPaths:
         [NSArray arrayWithObject:indexPath]
                 withRowAnimation:UITableViewRowAnimationFade];

编辑:

你的功能如下:

- (IBAction)contactDelete:(id)sender{
    [[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview];

    [self.tableView deleteRowsAtIndexPaths:
        [NSArray arrayWithObject:indexPath]
        withRowAnimation:UITableViewRowAnimationFade];
}