繁体   English   中英

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

[英]reload data in tableview in same screen

删除收藏夹表单元格中的联系人后如何重新加载数据(当我已经在收藏夹表中并且我应该重新加载我已经在观看的同一屏幕时,单击单元格中的删除按钮即可删除联系人)

favoriteTableViewController

//
//  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

最喜欢的TableViewCell

#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

您没有解释问题,但是在我看来,您应该从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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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