简体   繁体   English

ios:添加对象后,NSMutableArray Count打印零?

[英]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: 我的项目基于解析xml数据并将其添加到array并在各自的视图中显示,现在我的问题是解析xml并将这些对象添加到nsmutablearray,如下所示:

-(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 以上打印计数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: 但是,当我在tableview方法中调用那些Count时,它将输出0所以我无法在tableview中加载数据。这是我为tableview方法尝试的代码:

- (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 当运行上面的tableview代码时,第0部分根本没有加载,因为数组计数仅打印0第1部分正在加载,请帮助我如何解决此问题

Intialize sampleArray in ViewDidLoad 在ViewDidLoad中初始化sampleArray

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. 确保[tblView reloadData]正常工作。在connectionDidFinishLoading完成之前,将初始化表加载,因此count将为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: 并在.h文件中声明您的NSMutableArray,例如:

@property (nonatomic, strong) NSMutableArray *samplearray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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