0

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

18
  • after addobject in your array fix retain your array like this [yourarray retain]; i think this is problem . Commented Apr 4, 2014 at 10:17
  • whats the attribute of the property for sample array? is it strong or weak? Commented Apr 4, 2014 at 10:18
  • @DarshanKunjadiya I tries this [samplearray retain]; but still problem remains same Commented Apr 4, 2014 at 10:21
  • please put this line of code in viewDidLoad samplearray = [[NSMutableArray alloc]init]; Commented Apr 4, 2014 at 10:22
  • 1
    Bro i hope you are not deleting elements from self.samplearray anywhere else. Also check if [tblView reloadData] is working properly. Initialy table will be loaded before completion of connectionDidFinishLoading, so there count will be 0. Only in reload the count increments Commented Apr 4, 2014 at 11:00

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.