-3

我为 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;

合成为.m:

@synthesize theemplyees

然后在 ViewDidLoad 中,初始化并调用以填充它:

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 上向上或向下滚动,数组会显示数据为空……尽管它仍然显示记录数。如果我将表格中的单元格调整为更小,它将填充更多行(可见),但再次滚动并且它们消失了。

非常感谢任何想法或建议。提前感谢您的任何帮助!

地理...

4

1 回答 1

2

You all have weak properties which means that all of the strings are instantly deallocated, since nothing is retaining them. Change it to this and try:

@property (nonatomic, strong) NSString *theString;

Also, you want to initialize your NSMutableArray like so (you're missing the init method):

NSMutableArray *array = [[NSMutableArray alloc] init];
于 2013-03-30T00:34:50.020 回答