gpt4 book ai didi

ios - 以编程方式添加到 View Controller 的 UITableView 不会填充

转载 作者:行者123 更新时间:2023-11-28 22:05:36 24 4
gpt4 key购买 nike

我在我的 MatchCenterViewController 中以编程方式创建了一个 UITableView,但是它似乎没有填充我的云代码函数返回的 JSON 数据。它只是显示一个空白的 View Controller 。 MatchCenterViewController 是嵌入在导航 View Controller 中的 View Controller 。

MatchCenterViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"

@interface MatchCenterViewController : UIViewController <UITableViewDataSource>

@property (nonatomic) IBOutlet NSString *itemSearch;

@property (nonatomic, strong) NSArray *imageURLs;
@property (strong, nonatomic) NSString *matchingCategoryCondition;
@property (strong, nonatomic) NSString *matchingCategoryLocation;
@property (strong, nonatomic) NSNumber *matchingCategoryMaxPrice;
@property (strong, nonatomic) NSNumber *matchingCategoryMinPrice;

@property (strong, nonatomic) NSArray *matchCenterArray;

@end

MatchCenterViewController.m:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end

@implementation MatchCenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];
}
return self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [_matchCenter dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

NSDictionary *matchCenterDictionary= [self.matchCenterArray objectAtIndex:indexPath.row];

cell.textLabel.text = [matchCenterDictionary objectForKey:@"Title"];// title of the first object

// if([matchCenterDictionary objectForKey:@"Price"] != NULL)
// {
// cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[matchCenterDictionary objectForKey:@"Price"]];
// }

return cell;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.matchCenterArray = [[NSArray alloc] init];
}

- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [[NSArray alloc] init];

[PFCloud callFunctionInBackground:@"MatchCenterTest"
withParameters:@{
@"test": @"Hi",
}
block:^(NSDictionary *result, NSError *error) {

if (!error) {
self.matchCenterArray = [result objectForKey:@"Top 3"];

dispatch_async(dispatch_get_main_queue(), ^{
[_matchCenter reloadData];
});

NSLog(@"Test Result: '%@'", result);
}
}];
}

@end

最佳答案

如何创建这个 viewController?如果您不使用 [[MatchCenterViewController alloc] initWithNibName:... bundle:...] 将不会调用表创建。这可能会发生,因为您在 Storyboard中有 viewController,在这种情况下 initWithCoder: 将是要覆盖的方法。

我建议将这段代码移至 viewDidLoad,无论 viewController 是如何创建的,它都会被调用。例如:

- (void)viewDidLoad {
[super viewDidLoad];
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];

self.matchCenterArray = [[NSArray alloc] init];
}

关于ios - 以编程方式添加到 View Controller 的 UITableView 不会填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24112005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com