如何在.m文件的两种方法中使用NSMutableArray



我的.h文件中有:

 #import <UIKit/UIKit.h>
 #import "SQLClient.h"
 @interface mgrViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,
 SQLClientDelegate>{ 
     NSMutableArray *pajaros;
 }
 @property (weak, nonatomic) IBOutlet UITableView *miTabla;
 @property (nonatomic, retain)NSMutableArray *pajaros;
 @end

在我的.m文件中:

#import "mgrViewController.h"
#import "Vista2.h"
#import "SQLClient.h"
@interface mgrViewController ()
@end
@implementation mgrViewController
@synthesize miTabla;
@synthesize pajaros;
- (void)viewDidLoad
{
[super viewDidLoad];
SQLClient* client = [SQLClient sharedInstance];
client.delegate = self;
[client connect:@"xxx.xxx.xxx.xxx:xxxx" username:@"xxxxxxxxxxx" password:@"xxxxxxxxxxxx" database:@"xxxxxxxxxxx" completion:^(BOOL success) {
    if (success)
    {    
       pajaros =[[NSMutableArray alloc]init];
        [client execute:@"SELECT field FROM table WHERE field='xxxxxxxxx'" completion:^(NSArray* results) {          
               NSMutableString* resulta = [[NSMutableString alloc] init];
                for (NSArray* table in results)
                    for (NSDictionary* row in table)
                        for (NSString* column in row){
                            //[results appendFormat:@"n%@ = %@", column, row[column]];
                            [resulta appendFormat:@"n%@", row[column]];
                            [pajaros addObject:resulta];
                        }
            [client disconnect];
        }];
    }
}];
self.miTabla.delegate = self;
self.miTabla.dataSource = self;
}
 #pragma mark - SQLClientDelegate
- (void)error:(NSString*)error code:(int)code severity:(int)severity
{
    NSLog(@"Error #%d: %@ (Severity %d)", code, error, severity);
    [[[UIAlertView alloc] initWithTitle:@"Error" message:error delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
- (void)message:(NSString*)message
{
    NSLog(@"Message: %@", message);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return pajaros.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"celdaPajaros";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // elementos que contienen cada celda con sus tags
    UILabel *labelTitulo = (UILabel *) [cell viewWithTag:10];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    labelTitulo.text = [pajaros objectAtIndex:indexPath.row];
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70.f;
}
@end

我在-(void)viewDidLoad{}中为NSMutableArray pajaros添加了元素,我的问题是,如何在-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中使用我的NSMutableArray pajaros?。

谢谢:)

如果我在NSMutable Array上使用count并打印该数字,结果为1(因为我只选择了A数据),但前提是我使用:

...        
[pajaros addObject:resulta];
    }
     NSInteger num = pajaros.count;
     NSString *inStr = [NSString stringWithFormat: @"%ld", (long)num];
     self.textView.text = inStr;
     [client disconnect];
}];
...

但是,如果我在.m文件的另一部分执行计数,显示0,我接下来的问题就是为什么我的NSMutableArray只包含中的数据

 ...        
    [pajaros addObject:resulta];
        }
         [client disconnect];
    }];
    ...

谢谢你的帮助!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//此处的代码用于dequecellNSString*cellIdentifier=@"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell.textLabel.text = [pajaraos objectAtIndex:indexPath.row];
}
return cell;
}

相关内容

  • 没有找到相关文章

最新更新