简体   繁体   中英

ios: NSMutableArray Count Prints Zero after adding object?

My project is based on parsing xml data and adding it to array and display in respectives views,now my problem is am parsing xml and adding those objects it to nsmutablearray as shown below:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    samplearray = [[NSMutableArray alloc]init];
    xmlParserObject = [[NSXMLParser alloc] initWithData:webData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
     for (int i =0; i<[rssOutputData count]; i++) {
        NewsList *log = [rssOutputData objectAtIndex:i];
        feedid = log.id;
        NSLog(@"%d",feedid);
        Invit = log.newsletterdet;
        NSLog(@"%@",Invit);
         [samplearray addObject:log];
         NSLog(@"Count Final %d",[self.samplearray count]);

    }
    [[self navigationController] tabBarItem].badgeValue = mycount2;
    NSLog(@"%@",mycount2);
    [tblView reloadData];
    [connection release];

 }

The Above prints Count Value as 2014-04-04 15:21:10.009 cftsversion1[3087:70b] Count Final 1

But when I call those Count in tableview methods, it prints 0 so I cannot load datas in tableview Here is the code I tried for tableview methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
        return [samplearray count];Prints 0 here
    NSLog(@"Count %d",[samplearray count]); Prints 0 here
    if (section == 1)
        return 1;
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"eventCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    if (indexPath.section == 0)
    {
        NewsList *msglist = [samplearray objectAtIndex:indexPath.row];
        cell.textLabel.text = msglist.newsletterdet;
        NSLog(@"%@",msglist.newsletterdet);
        NSInteger stat = msglist.readflag;
        if ([[SingleTonClass sinlgeTon].colorArray2 containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] || stat ==  1) {
            cell.textLabel.textColor = [UIColor redColor];
        }
        else{
            cell.textLabel.textColor = [UIColor greenColor];
        }
        cell.backgroundColor = [UIColor blackColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    if (indexPath.section == 1)
    {
        UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewmoreButton.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
        [viewmoreButton setTitle:@"View More"  forState:UIControlStateNormal];
        [cell addSubview:viewmoreButton];
        [viewmoreButton addTarget:self
                           action:@selector(viewMore:)
                 forControlEvents:UIControlEventTouchUpInside];
        cell.backgroundColor = [UIColor blackColor];
        [cell.contentView addSubview:viewmoreButton];

    }
    return cell;
    }

When run the above tableview code section 0 is not at all loading because array count prints 0 only section 1 is loading please help me how to solve this issue Thanks in advance

Intialize sampleArray in ViewDidLoad

samplearray = [[NSMutableArray alloc]init]

Make sure [tblView reloadData] is working properly.Initialy table will be loaded before completion of connectionDidFinishLoading, so count will be 0. Only in reload the count increments.

I have doubt you are printing value in wrong way. Try to print it correctly first and update us:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
       NSLog(@"Count %d",[samplearray count]);\\ Prints 0 here    
       return [samplearray count];\\Prints 0 here
    } 
    else if (section == 1)
    {
        return 1;
    }

    return 0;
}

and declare your NSMutableArray in .h file like:

@property (nonatomic, strong) NSMutableArray *samplearray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM