从View Controller

时间:2018-02-27 16:46:57

标签: ios objective-c xcode uitableview uiviewcontroller

重新加载视图控制器最合适的方法是什么?

为了扩展这个场景,我有一个 UITableView ,它在视图控制器中填充了单元格。
我通过转换到新的“创建单元格”视图控制器来创建新单元格,但是当我调用dismissViewControllerAnimated函数时,新单元格不会出现。
我无法使用 segue ,因为初始视图控制器是标签栏视图控制器的一个组件,因此如果我从“创建单元格”视图控制器 segue ,则标签栏会消失。

那么在成功解雇“Create Cell”视图控制器后,如何重新加载视图?

提前致谢。

需要刷新的代码(实现了非工作通知方法):

@interface MyListings ()

@property (weak, nonatomic) IBOutlet UIButton     *createListingButton;
@property (weak, nonatomic) IBOutlet UITableView  *tableView;
@property (strong, nonatomic)        ListingModel *listItem;
@property (strong, nonatomic)        UserModel    *usr;

@end

@implementation MyListings

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(newCellCreated:)
                                                 name:@"newCellCreated"
                                               object:nil];

    self.listItem = [[ListingModel alloc] init];
    self.usr      = [[UserModel alloc]init];

    NSDate *currentDateTime = [NSDate date];

    FIRUser *user = [FIRAuth auth].currentUser;

    if ([self.usr getDataForUser:user.email])
    {
        if ([self.listItem getDataForUser:[NSString stringWithFormat:@"%d", self.usr.user_id]])
        {
            NSLog(@"Got listings");
        };
    }

    titles = self.listItem.title;
    currentBids = self.listItem.starting_bid;
    stillAvailables = self.listItem.timer;
    listingIDs = self.listItem.listing_id;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd"];

    for(int idx = 0; idx < [self.listItem.listing_id count]; idx++)
    {
        NSDate *d1 = [dateFormatter dateFromString:self.listItem.timer[idx]];
        if([d1 compare:currentDateTime] == NSOrderedDescending)
        {
            stillAvailables[idx] = @"Expired";
        }
        else
        {
            stillAvailables[idx] = @"Available";
        }
    }

    _createListingButton.layer.cornerRadius = 8;
    _createListingButton.layer.borderWidth = 1.5f;
    _createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
    [_createListingButton addTarget:self action:@selector(createListingButtonHighlightBorder) forControlEvents:UIControlEventTouchDown];
    [_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchUpInside];
    [_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchDragExit];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void) newCellCreated:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"newCellCreated"])
        [self.tableView reloadData];
}

- (void)createListingButtonHighlightBorder
{
    _createListingButton.layer.borderColor = [UIColor colorWithRed:0.61 green:0.00 blue:0.02 alpha:1.0].CGColor;
}

- (void)createListingButtonUnhighlightBorder
{
    _createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyListingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingsTableViewCell"];
    [cell updateCellWithTitle:[titles objectAtIndex:indexPath.row] currentBid:[currentBids objectAtIndex:indexPath.row] stillAvailable:[stillAvailables objectAtIndex:indexPath.row] listingID:[listingIDs objectAtIndex:indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"editListing" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"editListing"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        EditListing *destController = segue.destinationViewController;
        destController.listId = self.listItem.listing_id[indexPath.row];
    }
}

1 个答案:

答案 0 :(得分:0)

您可以拥有通知或委托方法。

由于您没有发布任何代码,请将名称替换为方便。

我将使用通知给你一个简短的例子。

在TableViewController中:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "newCellCreated"), object: nil, queue: nil) { (notification) in
        self.tableView.reloadData()
    }
}

在CreateCellViewController中:

func closeView() {
    self.dismiss(animated: true) {
        NotificationCenter.default.post(NSNotification.Name(rawValue: "newCellCreated"))
    }
}

编辑 - 目标C版

在TableViewController中:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(newCellCreated:)
                                                 name:@"newCellCreated"
                                               object:nil];
}

- (void) newCellCreated:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"newCellCreated"])
        [self.tableView reloadData];
}

在CreateCellViewController中:

-(void) closeView {
    [self dismissViewControllerAnimated:NO completion:^{
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"newCellCreated"
         object:self];
    }];
}
相关问题