【发布时间】:2018-08-07 09:39:48
【问题描述】:
重新加载我的视图控制器最合适的方法是什么?
为了扩展该场景,我有一个 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];
}
}
【问题讨论】:
-
你试过使用通知吗?
-
我该怎么做?
-
您可以使用多种不同的方法来实现您想要的。您可以创建
delegate方法、NSNotification或回调block。请向我们展示一些代码,我们可以看到其中最适合您的方法。 -
我的电脑目前处于故障状态。目前,我真的无法添加任何代码。但是,为了描述视图控制器,它只包含实例化原型单元所必需的内容,这些单元位于表格视图中,其属性等同于各种数组的元素。因此,它是一个简单的表格视图类,其中包含表格视图单元格。
标签: ios objective-c xcode uitableview uiviewcontroller