无法在tableview中显示数据

时间:2023-01-15 19:44:13

I'm fetching data from server and unable to display it in tableview ...i'm storing data in jsonarray variable..but its showing jsonarray.count as zero outside completion handler. (in no of rows numberOfRowsInSection).

我从服务器获取数据并且无法在tableview中显示它...我将数据存储在jsonarray变量中..但它将jsonarray.count显示为零外部完成处理程序。 (没有行numberOfRowsInSection)。

@interface Staffdir ()
@property(nonatomic,strong) NSMutableArray* jsonarr;
@end

@implementation Staffdir

- (void)viewDidLoad {

    _namesarr=[[NSMutableArray alloc]init];
    _occupationarr=[[NSMutableArray alloc]init];
    _Phonearr=[[NSMutableArray alloc]init];
    _faxnumarr=[[NSMutableArray alloc]init];

    NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init];
    NSURL*url=[NSURL URLWithString:@"http:someurl"];
    [req  setURL:url];


        [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            self.jsonarr = [NSJSONSerialization JSONObjectWithData:data
                                                           options:kNilOptions
                                                             error:&error];

            NSDictionary *dict=[[NSDictionary alloc]init];

            for(int i=0;i<self.jsonarr.count;i++){

                dict=[self.jsonarr objectAtIndex:i];

                [_namesarr addObject:[dict objectForKey:@"Name"]];
                [_faxnumarr addObject:[dict objectForKey:@"Fax_Number"]];
                [_Phonearr addObject:[dict objectForKey:@"Phone"]];
                [_occupationarr addObject:[dict objectForKey:@"Job_Title"]];
            }
        }] resume];


    [self.tableview reloadData];
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

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

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

    Staffcustom *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) {
        cell = [[Staffcustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    cell.namelabel.text=[_namesarr objectAtIndex:indexPath.row];
    cell.namelabel.textColor = [UIColor blueColor];
    cell.occupationlabel.textColor=[UIColor colorWithRed:0.5 green:0.0 blue:1 alpha:1.0];
    cell.occupationlabel.text=[_occupationarr objectAtIndex:indexPath.row];
    cell.phonelabel.text=@"12345678";
    cell.faxnumlabel.text=@"12345678";

    UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,0.1)];
    additionalSeparator.backgroundColor = [UIColor blueColor];

    [cell addSubview:additionalSeparator];

    return cell;
}

- (IBAction)Btn_Back:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

1 个解决方案

#1


1  

try this :

尝试这个 :

geting data from the server

从服务器获取数据

    [json PostMethod:method parameters:parameter success:^(NSDictionary *responseDict){

        response=[[NSMutableArray alloc]init];
        for (int i=0; i < [[responseDict valueForKey:@"transactions"] count]; i++) {
            [response addObject:[[responseDict valueForKey:@"transactions"]objectAtIndex:i]];
        }

        [self.tableView reloadData];

TableviewCell Format

TableviewCell格式

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

    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

        if ([response count]  > 0) {
             //creating the status lable
            UILabel * name = [[UILabel alloc]initWithFrame:CGRectMake( 80*XScale, 25*YScale, XScale*200, YScale*15)];

            name.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:12.0f];
            NSString *time=[[response objectAtIndex:indexPath.row]valueForKey:@“name”];
            [cell.contentView addSubview:name];

check it ,if u still facing error update me .i will helps ,if u provide ur response it will helps me to resolve issue easy.

检查它,如果你仍然面临错误更新我.i会有所帮助,如果你提供你的回复它将帮助我轻松解决问题。

#1


1  

try this :

尝试这个 :

geting data from the server

从服务器获取数据

    [json PostMethod:method parameters:parameter success:^(NSDictionary *responseDict){

        response=[[NSMutableArray alloc]init];
        for (int i=0; i < [[responseDict valueForKey:@"transactions"] count]; i++) {
            [response addObject:[[responseDict valueForKey:@"transactions"]objectAtIndex:i]];
        }

        [self.tableView reloadData];

TableviewCell Format

TableviewCell格式

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

    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

        if ([response count]  > 0) {
             //creating the status lable
            UILabel * name = [[UILabel alloc]initWithFrame:CGRectMake( 80*XScale, 25*YScale, XScale*200, YScale*15)];

            name.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:12.0f];
            NSString *time=[[response objectAtIndex:indexPath.row]valueForKey:@“name”];
            [cell.contentView addSubview:name];

check it ,if u still facing error update me .i will helps ,if u provide ur response it will helps me to resolve issue easy.

检查它,如果你仍然面临错误更新我.i会有所帮助,如果你提供你的回复它将帮助我轻松解决问题。