简体   繁体   English

我在表格视图中的输出未正确显示输出

[英]My output in table view is not displaying output correctly

Even after giving constrain, My output is not as per my expectation, The labels are getting overlapped and my image is shifted to left. 即使施加了约束,我的输出也没有达到我的期望,标签变得重叠,并且我的图像向左移动。

My code snippet - 我的代码段-

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

the output is like this- 输出是这样的- 在此处输入图片说明

我认为您需要像这样在viewController中设置tableview数据源。

tableView.dataSource = self

The problem is, you got an instance of UITableViewCell instead of SimpleTableCell when dequeuing the cell. 问题是,在使单元出队时,您获得了UITableViewCell的实例而不是SimpleTableCell的实例。

Just a guess. 只是一个猜测。 Try Replace this snippet: 尝试替换此代码段:

[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];
}

with: 与:

SimpleTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];

When using storyboard designs, the prototyping cell can be accessible without register. 使用情节提要设计时,无需注册即可访问原型单元。

Also, if above not working, try restarting XCode. 另外,如果上述方法不起作用,请尝试重新启动XCode。 Sometimes i got mess with XCode file indexing. 有时我对XCode文件索引感到困惑。

You should remove this line: 您应该删除以下行:

 [self.emplyoyeeTableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"SimpleTableCell"];

Because you create the SimpleTableCell inside your viewController already, so there is no need to registerNib. 因为您已经在viewController内部创建了SimpleTableCell ,所以不需要registerNib。

Try This Code:- 试试这个代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM