【问题标题】:UITableView in UIViewController not displaying json?UIViewController中的UITableView不显示json?
【发布时间】:2014-12-05 17:32:02
【问题描述】:

我正在使用 storyboards 构建一个 iOS 应用程序。我在 uiviewcontroller 中有 uitableview 并解析在表格单元格中显示的 json 数据。但我遇到了一个问题,我正在获取 json(userid) 但它没有显示在表格单元格。我在表格单元格中有一个 iboutlet 标签,用于显示用户 ID。

这是我的代码: 在view.h

@interface HostDetail : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *UserId;

在视图中.m

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[_eventTable reloadData];

_UserId = [[NSMutableArray alloc] init];
NSURL *url=[NSURL URLWithString:@"http://34.100.29.667:3565/api/user/_id/host/:_id/joinrequest"];
NSData* data = [NSData dataWithContentsOfURL:url];
if ((unsigned long)data.length > 3) {
    // NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  if(ys_avatars){
    for (int j=0;j<ys_avatars.count;j++)
        {
        if( ys_avatars[j][@"userid"]==nil ){
             [_UserId addObject: @""];
      }
            else{
                [_UserId addObject:ys_avatars[j][@"userid"]];}
        }}}
else
{
    NSLog(@"asd");
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return _UserId.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *Cellidentifier1 = @"eventList";
HostTableCell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
 long row = [indexPath row];
cell1.userid.text = _UserId[row];
return cell1;
}

【问题讨论】:

  • 所以你已经验证你得到了 JSON,它反序列化没有错误(即使你忽略了 error: 参数),并且数据出现在你的 _UserId 数组中?您是否在方法中设置了断点以查看是否正在调用 numberOfRowsInSectioncellForRowAtIndexPath
  • @HotLicks 是的,我已经在这些 numberOfRowsInSection 和 cellForRowAtIndexPath 中放置了断点,但是我没有调用它,因为我试图找出为什么代码不在这些方法中。
  • 您是否在 _UserId[row] 上进行 NSLog 以确保它是一个数字?
  • 那么,你为table view设置了delegate和dataSource指针了吗?
  • Hot Licks 是的,我在 .h @interface HostDetail 中设置了代表:UIViewController

标签: ios objective-c json uitableview uiviewcontroller


【解决方案1】:

我已经通过在

中设置代表解决了我的问题
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2021-10-31
    相关资源
    最近更新 更多