如何从NSMutableArray中显示UILabel中的数据?

时间:2014-10-22 15:23:36

标签: ios uitableview 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

如果我在代码行pajaros之后为我的NSMutableArray [pajaros addObject:resulta];添加一个计数并打印该计数,则结果为1,因为我的条件用于选择数据。但如果在我的代码的其他部分计数,结果为0。

我的问题是我如何在NSMutableArray中保留数据以供用于:

  -(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;
    }

感谢您的帮助,我是Objective-C的新手。

如果我使用以下数据初始化pajaros

- (void)viewDidLoad
    {
    [super viewDidLoad];
    pajaros = [NSMutableArray arrayWithObjects:@"Bird1", nil];
    self.miTabla.delegate = self;
    self.miTabla.dataSource = self;
    }

运行我的应用程序,标签Titulo告诉我Bird1。我的第二个问题是:为什么当我从数据库向我的NSMutableArray pajaros添加数据并运行我的应用程序时,它什么都没有显示?

感谢。

1 个答案:

答案 0 :(得分:0)

使用属性时,最好包含self.。在您使用self.pajaros = [[NSMutableArray alloc]init];的其他地方尝试pajaros