Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi Friensd 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 above

-(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 able to 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

share|improve this question
    
is ur code under ARC? –  Shanti K 2 days ago
    
retain your array and then try. –  Darshan Kunjadiya 2 days ago
    
@DarshanKunjadiya Little clear please –  user3387132 2 days ago
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 –  DILi 2 days ago
1  
Bro if it helps, please accept this as answer. Happy Coding!! –  DILi 2 days ago
show 19 more comments

2 Answers

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.

share|improve this answer
add comment

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;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.