即使给出约束,我的输出也不符合我的预期,标签重叠,我的图像向左移动。
我的代码段 -
- (void)viewDidLoad {
[super viewDidLoad];
// [self.emplyoyeeTableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"SimpleTableCell"];
NSManagedObjectContext *manageobjectcontext = [self manageobjectcontext];
NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]initWithEntityName:@"Employee"];
self.employeeArray = [[manageobjectcontext executeFetchRequest:fetchrequest error:nil]mutableCopy];
[self.emplyoyeeTableView reloadData];
}
#pragma mark - <UITableViewDataSource>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.employeeArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 110.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SimpleTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];
NSManagedObject *emp = [self.employeeArray objectAtIndex:indexPath.row];
[cell.nameLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"name"]]];
[cell.emailLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"email"]]];
[cell.designationLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"designation"]]];
[cell.picImageView setImage:[UIImage imageNamed:@"skinage 2.png"]];
return cell;
}
答案 0 :(得分:0)
我认为您需要在viewController中设置tableview数据源,如下所示。
tableView.dataSource = self
答案 1 :(得分:0)
问题是,当您将单元格出列时,您获得了UITableViewCell
而不是SimpleTableCell
的实例。
只是一个猜测。尝试 替换此代码段:
[self.emplyoyeeTableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"SimpleTableCell"];
SimpleTableCell *cell = (SimpleTableCell *) [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
使用:
SimpleTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];
使用故事板设计时,无需注册即可访问原型单元。
此外,如果上面不起作用,请尝试重新启动XCode。有时我弄乱了XCode文件索引。
答案 2 :(得分:0)
你应该删除这一行:
[self.emplyoyeeTableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"SimpleTableCell"];
因为你已经在viewController中创建了 SimpleTableCell ,所以不需要registerNib。
答案 3 :(得分:0)
尝试此代码: -
- (void)viewDidLoad {
[super viewDidLoad];
[self.emplyoyeeTableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"SimpleTableCell"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *) [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.clipsToBounds=YES;
NSManagedObject *emp = [self.employeeArray objectAtIndex:indexPath.row];
[cell.nameLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"name"]]];
[cell.emailLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"email"]]];
[cell.designationLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"designation"]]];
[cell.picImageView setImage:[UIImage imageNamed:@"skinage 2.png"]];
return cell;
}