NSMutableArray不维护数据

时间:2013-03-30 00:31:53

标签: iphone objective-c xcode

我为EmployeeInfo创建了一个自定义类:

@interface METEmployee : NSObject
@property (weak, nonatomic) NSString *EmpNo;
@property (weak, nonatomic) NSString *FirstName;
@property (weak, nonatomic) NSString *MiddleName;
@property (weak, nonatomic) NSString *LastName;
@property (weak, nonatomic) NSString *Department;
@property (weak, nonatomic) NSString *HireDate;
@property (weak, nonatomic) NSString *JobTitle;

@property (weak, nonatomic) NSString *SupervisorNumber;
@property (weak, nonatomic) NSString *SupLastName;
@property (weak, nonatomic) NSString *SupFirstName;
@property (weak, nonatomic) NSString *MobilePhone;
@property (weak, nonatomic) NSString *EmailAddress;
@property (weak, nonatomic) NSString *DeskPhone;

@property (weak, nonatomic) NSString *EmployeeType;
@property (weak, nonatomic) NSString *Location;
@end

然后在我的视图controller.h中创建:

NSMutableArray *theemplyees;
@property(nonatomic, retain) NSMutableArray *theemplyees;

Synthesize是.m:

@synthesize theemplyees

然后在ViewDidLoad中,init并调用以填充它:

theemplyees = [[NSMutableArray alloc] init];
[self getEmps];  

在getEmps中,我将记录添加到数组中:

        results = [database executeQuery:@"select * from EmpInfo order by LastName"];
    while([results next]) {

        METEmployee *empInfo = [METEmployee alloc];
        empInfo.EmpNo = [results stringForColumn:@"EmpNo"];
        empInfo.FirstName = [results stringForColumn:@"FirstName"];
        empInfo.MiddleName = [results stringForColumn:@"MiddleName"];
        empInfo.LastName = [results stringForColumn:@"LastName"];
        empInfo.Department = [results stringForColumn:@"Department"];
        empInfo.HireDate = [results stringForColumn:@"HireDate"];
        empInfo.JobTitle = [results stringForColumn:@"JobTitle"];
        empInfo.SupervisorNumber = [results stringForColumn:@"SupervisorNumber"];
        empInfo.SupFirstName = [results stringForColumn:@"SupFirstName"];
        empInfo.SupLastName = [results stringForColumn:@"SupLastName"];
        empInfo.MobilePhone = [results stringForColumn:@"MobilePhone"];
        empInfo.EmailAddress = [results stringForColumn:@"EmailAddress"];
        empInfo.DeskPhone = [results stringForColumn:@"DeskPhone"];
        empInfo.EmployeeType = [results stringForColumn:@"EmployeeType"];
        empInfo.Location = [results stringForColumn:@"Location"];
        [theemplyees addObject:empInfo];
    }

一切正常,可以为第一个充满记录的屏幕填充表格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  METEmpTableViewCell *cell = (METEmpTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"myCell"];

cell.lblFirstName.text = [[theemplyees objectAtIndex:indexPath.row] valueForKey:@"FirstName"];
cell.lblLastName.text = [[theemplyees objectAtIndex:indexPath.row] valueForKey:@"LastName"];
cell.lblDepartment.text = [[theemplyees objectAtIndex:indexPath.row] valueForKey:@"Department"];

 NSString* picName = [NSString stringWithFormat:@"%@.jpg", [[theemplyees objectAtIndex:indexPath.row] valueForKey:@"EmpNo"]];
@try {
    cell.empPicture.image = [UIImage imageNamed:picName];
}
@catch (NSException *exception) {

}
@finally { 
}

return cell;

}

但是,如果我尝试在Tableview上向上或向下滚动,则数组表示数据为空...尽管它仍显示记录计数。如果我将我的单元格调整为较小的表格,它将填充更多的行(可见)但是再次滚动它们就会消失。

非常感谢任何想法或建议。在此先感谢您的帮助!

地理......

1 个答案:

答案 0 :(得分:2)

你们都有弱属性,这意味着所有字符串都会被立即释放,因为没有任何东西可以保留它们。将其更改为此并尝试:

@property (nonatomic, strong) NSString *theString;

另外,你想要像这样初始化你的NSMutableArray(你错过了init方法):

NSMutableArray *array = [[NSMutableArray alloc] init];