簡體   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