简体   繁体   中英

reload data in tableview in same screen

How can I reload data after delete contacts in favoriteTable cell (I delete contacts clicking on delete button in the cell when i am already in favoriteTab and i should reload the same screen i'm already watching)

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

You did not explain the problem, but it seems to me you should delete the deleted contact from myFavListContactsData array datasource, then you can reload.

A delegate would be appropriate for this situation.

You don't have to reload the tableView, once deleted from the datasource, you can remove the cell from the tableView :

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

EDIT:

Your function would look like this:

- (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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM