Table View尝试读取时,NSMutablearray为空

时间:2013-03-16 15:50:50

标签: ios uitableview nsmutablearray

现在我收到有关尝试从对象读取数据的表视图单元格的错误。我正在访问一个数据库,将所有行存储到一个NSMUtable数组作为对象,似乎工作正常。但是当我希望表视图读取该数组时,我一直得到'null'。任何帮助将不胜感激。

ERROR

  

- [AttendeeData isEqualToString:]:无法识别的选择器发送到实例0x751ca80   2013-03-16 11:40:52.499 ExpoRequestLite [39716:c07] * 由于未捕获的异常'NSInvalidArgumentException'终止应用,

@interface DashLandingViewController ()

@end

@implementation DashLandingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self){
            // Custom initialization
        }
        return self;
}

- (void)viewDidLoad {

       [super viewDidLoad];

        entries  = [[NSMutableArray alloc]init];
        _attendeeDataToPush = [[AttendeeData alloc]init];

        [self openDB];

        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM data"];
        sqlite3_stmt *statement;

        if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK) {
            while(sqlite3_step(statement) == SQLITE_ROW){
                NSLog(@"got all attendees");

               NSString *firstName = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];

                _attendeeDataToPush.firstNameValue = firstName;

                [entries addObject:_attendeeDataToPush];
                NSLog(@"array has %@", [[entries objectAtIndex:0] firstNameValue]);

           }
      }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [entries count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
       NSLog(@"selected firstname is %@", [[entries objectAtIndex:0] firstNameValue]);
        cell.textLabel.text = [entries objectAtIndex:indexPath.row];
        return cell;
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
}


@end

2 个答案:

答案 0 :(得分:1)

错误表示您的代码中的其他位置有错误,请发布完整的代码。根据您的错误,您似乎在对象上调用isEqualToString:而非字符串类型。您的代码未显示您收到错误的位置。对于使用isEqualToString:,两个对象都应为NSString类型。如果要将对象与字符串进行比较,请改用isEqual:

答案 1 :(得分:0)

此处可能发生错误

 NSLog(@"selected firstname is %@", [[entries objectAtIndex:0] firstNameValue]);
 cell.textLabel.text = [entries objectAtIndex:indexPath.row];

将这些行修改为,

 if([entries count] > indexPath.row){

     AttendeeData *attendeeData = [entries objectAtIndex:indexPath.row];
     NSLog(@"selected firstname is %@", attendeeData.firstNameValue);
    cell.textLabel.text = attendeeData.firstNameValue;
}