簡體   English   中英

使用objective-c將值存儲在json數組中時,在tableview中顯示為無數據

[英]Display as no data in tableview when the value stored in json array using objective-c

我是iOS開發的新手。 我遇到了一個問題。在下面的代碼中,我使用了api來獲取數據。 問題:UITableView中未顯示數據。 這是我的代碼

- (MXSegmentedPager *)segmentedPager {
    if (!_segmentedPager) {

        // Set a segmented pager below the cover
        _segmentedPager = [[MXSegmentedPager alloc] init];
        _segmentedPager.delegate    = self;
        _segmentedPager.dataSource  = self;

        self.tableView1.alwaysBounceVertical = NO;
    }
    return _segmentedPager;
}
    - (UITableView *)tableView {
    if (!_tableView) {
        //Add a table page
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        self.tableView.separatorColor = [UIColor clearColor];
        self.tableView.alwaysBounceVertical = NO;
    }
    return _tableView
    ;
}
- (UITableView *)tableView2 {
    if (!_tableView2) {
        //Add a table page
        _tableView2 = [[UITableView alloc] init];
        _tableView2.delegate = self;
        _tableView2.dataSource = self;

        self.tableView2.alwaysBounceVertical = NO;
        _tableView2.tableFooterView=[UIView new];
    }
    return _tableView2
    ;
}

#pragma mark <MXSegmentedPagerDelegate>

- (CGFloat)heightForSegmentedControlInSegmentedPager:(MXSegmentedPager *)segmentedPager {
    return 30.f;
}

- (void)segmentedPager:(MXSegmentedPager *)segmentedPager didSelectViewWithTitle:(NSString *)title {
    //  NSLog(@"%@ page selected.", title);
}

- (void)segmentedPager:(MXSegmentedPager *)segmentedPager didScrollWithParallaxHeader:(MXParallaxHeader *)parallaxHeader {
    // NSLog(@"progress %f", parallaxHeader.progress);
}

#pragma mark <MXSegmentedPagerDataSource>

- (NSInteger)numberOfPagesInSegmentedPager:(MXSegmentedPager *)segmentedPager {
    return 2;
}

- (NSString *)segmentedPager:(MXSegmentedPager *)segmentedPager titleForSectionAtIndex:(NSInteger)index {
    return @[@"Profile", @"Room Details"][index];
}

- (UIView *)segmentedPager:(MXSegmentedPager *)segmentedPager viewForPageAtIndex:(NSInteger)index {
    //    return NO;
    return @[self.tableView, self.tableView1][index];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    if (tableView==_tableView) {
        return 1;
    }


   else    if (tableView==_tableView1) {
        return [[Dict objectForKey:@"room_details"]count];
    }
else
        return 1;

}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView==_tableView) {
        return 7;
    }
    else if(tableView==_tableView1)
        {
        return [[Dict objectForKey:@"accom_profile"]count];     
        }
else
        return NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView==_tableView) {
        static NSString *CellIdentifier = @"cel";
        cell1=[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell1==nil) {
            NSArray*toplevelobject=[[NSBundle mainBundle] loadNibNamed:@"prof_cel1" owner:self options:nil];
            cell1=[toplevelobject objectAtIndex:0];
        }
               if (indexPath.row==0) {

            NSString *s1=[[[Dict objectForKey:@"accom_profile"] objectAtIndex:0] objectForKey:@"acc_building_no"];
            cell1.tit_labl.text=@"Buliding No";
             if (s1==(id)[NSNull null] || [s1 isEqualToString:@""])
            {
              cell1.sub_labl.text=@"-";
            }
            else{
               cell1.sub_labl.text=s1;
            }
        }

        return cell1;
    }

    else if (tableView==_tableView1)
    {
        static NSString *CellIdentifier = @"cel";

        room=[self.tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (room==nil) {
            NSArray*toplevelobject=[[NSBundle mainBundle] loadNibNamed:@"room_details" owner:self options:nil];
           room=[toplevelobject objectAtIndex:0];
        } 
        NSString *s1=[[[Dict objectForKey:@"room_details"]objectAtIndex:indexPath.section]objectForKey:@"intake_capacity"];
        if (s1==(id)[NSNull null] || [s1 isEqualToString:@""]) {  
            room.intake.text=@"-";
        }
        else {
           room.intake.text=s1;
        }
        return  room;
    }   
 else
        return  nil;
}

這樣我得到了輸出。 我的api格式如下。

 "accom_profile" =     (
                {
            "acc_address" = "";
            "acc_building_no" = 1; }
    );
 "room_image" =     ({
            "file_path" = "http:example.com";
    );

})

但是當api是這種方式時,

 "accom_profile" =     (
                {
            "acc_address" = "";
            "acc_building_no" = 1; }
    );
 "room_image" =     (
    );

在tableview2的Dictionary中的值為空值。 當我在index1選擇segmentedpager時,如何在表視圖中顯示為無數據?

"accom_profile" =     (
                {
            "acc_address" = "";
            "acc_building_no" = 1; }
    );
 "room_image" =     (
    );

這是JSON數據,我假設您將其放在字典中,例如NSDictionary * dictionary

key room_image包含一個NSArray 拿來並放入數組說

NSArray * arrayRoomImage = [dictionary valueForKey:@"room_image"];

UITableView DataSource方法中

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(arrayRoomImage.count!=0) // Checking is array is Not empty
return arrayRoomImage.count;

return 1; // executes only if array empty 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(arrayRoomImage.count!=0){ // Checking is array is Not empty
// return the cell when room_image is not empty
}
// Code here will execute only if arrayRoomImage is empty
// return another cell which has a label with text No Data.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM