UITableViewCell ios的错误

时间:2016-07-29 05:22:33

标签: ios objective-c uitableview

我正在开发iOS项目。我创建了一个UITableView来显示无人机列表。一切正常但我注意到当我点击单元格显示无人机的细节时有一个错误。正如你在下面的图片中看到的那样。

当tableView被屏蔽

enter image description here

点击单元格

enter image description here

这是我选择单元格时的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *_storyboardName = @"Main";
    UIStoryboard *_storyboard = [UIStoryboard storyboardWithName:_storyboardName bundle: nil];
    DroneDetailsViewController *detailsVC = [_storyboard instantiateViewControllerWithIdentifier:@"DroneDetailsViewController"];
    detailsVC.droneObj = [drones objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailsVC animated:YES];
    [detailsVC.navigationController setNavigationBarHidden:NO animated:YES];
}

这是我的cellForRowAtIndexPath代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"drone";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    DroneObject *droneObjDisplay = nil;
    droneObjDisplay = [drones objectAtIndex:indexPath.row];
    NSData *data = [[NSData alloc]initWithBase64EncodedString:droneObjDisplay.modelMSide options:NSDataBase64DecodingIgnoreUnknownCharacters];
    [self initCellWithComponents:cell :data :droneObjDisplay.modelName :droneObjDisplay.modelNumber];
    return cell;
}

这是initCelWithComponents:

- (void)initCellWithComponents :(UITableViewCell *)cell :(NSData *)dataImage :(NSString *)title :(NSString *)subTitle {

    UIView *viewCell = [[UIView alloc] initWithFrame:CGRectMake(VIEW_MARGIN, VIEW_MARGIN, widthtScreen-20, VIEW_IMAGE_HEIGHT)];
    viewCell.layer.borderColor = [UIColor colorWithHexString:NSLocalizedString(@"border_color", nil)].CGColor;
    viewCell.backgroundColor = [UIColor whiteColor];
    viewCell.layer.borderWidth = 0.5f;

    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(VIEW_MARGIN,VIEW_MARGIN, VIEW_IMAGE_HEIGHT-(2*VIEW_MARGIN),VIEW_IMAGE_HEIGHT-(2*VIEW_MARGIN))];
    image.image=[UIImage imageWithData:dataImage];
    [viewCell addSubview:image];

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+2*VIEW_MARGIN, VIEW_MARGIN, viewCell.frame.size.width-(VIEW_IMAGE_HEIGHT-(4*VIEW_MARGIN)), 21)];
    titleLabel.text = title;
    titleLabel.textColor = [UIColor blackColor];
    [titleLabel setFont:[UIFont systemFontOfSize:15]];
    [viewCell addSubview:titleLabel];

    UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+2*VIEW_MARGIN, 3*VIEW_MARGIN, viewCell.frame.size.width-(VIEW_IMAGE_HEIGHT-(4*VIEW_MARGIN)), 21)];
    subTitleLabel.text = subTitle;
    subTitleLabel.textColor = [UIColor blackColor];
    [subTitleLabel setFont:[UIFont systemFontOfSize:13]];
    [viewCell addSubview:subTitleLabel];

    cell.contentView.backgroundColor = [UIColor colorWithHexString:NSLocalizedString(@"gray_background", nil)];
    [cell.contentView addSubview:viewCell];
}

任何想法如何解决?!

4 个答案:

答案 0 :(得分:1)

您的手机样式为UITableViewCellStyleSubtitle,您也可以手动添加标题和字幕标签。

如果您使用单元格的样式为UITableViewCellStyleSubtitle,则可以直接使用

 cell.textLabel.text = @"your title label's text";
cell.detailTextLabel.text = @"your detail label's text";

如果你要添加另一个标签,那么它会创建重复!!!!

答案 1 :(得分:0)

由于您在cellForRowAtIndexPath中的方法中初始化UIView和UILabel并将它们添加为子视图,因此每次重新加载单元格时都会将这些控件添加到您的单元格中。下面的代码使用唯一标识符标识每个单元格,并仅在UITableView中没有该特定单元格时加载单元格 我编写了droneObjDisplay.droneId来唯一标识单元格,你可以为droneObjDisplay .__写任何东西,它是你对象的唯一标识符。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
  DroneObject *droneObjDisplay = nil;
  droneObjDisplay = [drones objectAtIndex:indexPath.row];
  NSString *CellIdentifier = [NSString stringWithFormat:@"drone_%d", droneObjDisplay.droneId];
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) 
  {  
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    NSData *data = [[NSData alloc]initWithBase64EncodedString:droneObjDisplay.modelMSide options:NSDataBase64DecodingIgnoreUnknownCharacters];
   [self initCellWithComponents:cell :data :droneObjDisplay.modelName :droneObjDisplay.modelNumber];
  }
  return cell;
}  

希望它有所帮助。

答案 2 :(得分:0)

可以通过以下任一方式设计和使用TableViewCell,

  1. 如果您正在使用故事板,那么您可以简单地在故事板上的TableView上设计原型单元格,然后为单元格提供标识符。如果您使用这种方式,那么只在故事板上添加组件,并为每个组件添加标签,以便在您不是UITableViewCell的子类时将代码访问它,并按如下方式将单元格出列,

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    UILabel *labelValue = (UILabel*) [cell viewWithTag:10];
    
  2. 在故事板上设计TableViewCell,就像在单向中一样,然后简单地创建一个UITableViewCell类的子类,并通过在故事板上选择单元格将该类设置为单元格的自定义类,这样您就可以创建单元中每个组件的出口,您不需要通过标签访问组件。下面是将单元格出列的代码,

    CellSubClassed *cell = (CellSubClassed*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.labelXYZ.text = arrayDataSource[indexPath.row][@"key"];
    
  3. 这是故事板介绍之前的旧方法,创建UITableView单元的子类并创建一个与UiTableViewCell子类同名的xib文件,创建要访问的组件的出口。下面是出列单元格的代码,

    NSArray *nib;
    static NSString *strNibName = @"CustomCell";
    
    nib = [[NSBundle mainBundle] loadNibNamed:strNibName owner:self options:nil];
    
    CustomCell *cell = [nib objectAtIndex:0];
    cell.imageView.image = arrayDataSource[indexPath.row];
    
  4. 最后一种方法是使用TableView的默认单元格,只需在storyboard / xib上创建tableView并将单元格出列如下,

    static NSString *cellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text = @"xyz";
    
  5. 默认的tableView单元格样式为

    • UITableViewCellStyleDefault
    • UITableViewCellStyleSubtitle
    • UITableViewCellStyleValue1
    • UITableViewCellStyleValue2

    您可以阅读有关Apple Developer docs here的更多信息。

    因此,在您的情况下,您混合使用两种方法将单元格出列,如果您想要像手动那样手动添加组件,并且您不想在故事板或Xib上设计组件,那么您只需创建UITableView单元的子类,然后在awakeFromNib方法中创建组件,并按照3方式解释出列单元格。

    希望它对你有所帮助。

答案 3 :(得分:0)

好的,最后,在得到你的答案之后,我创建了一个自定义单元格UITableViewCell的子类,我在那里添加了所有组件。主要问题是如何在没有UI(storyboard或xib)的情况下通过代码调用它。 因此,在创建自定义单元格后,我将此行添加到我的viewdidload:

[self.tableView registerClass:[CustomCellTableViewCell class] forCellReuseIdentifier:@"CustomCellTableViewCell"];

然后在cellForRowAtIndexPath中我刚刚添加了自定义单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    DroneObject *droneObjDisplay = nil;
    droneObjDisplay = [drones objectAtIndex:indexPath.row];
    static NSString *identifier = @"CustomCellTableViewCell";
    CustomCellTableViewCell *cell = (CustomCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    NSData *data = [[NSData alloc]initWithBase64EncodedString:droneObjDisplay.modelMSide options:NSDataBase64DecodingIgnoreUnknownCharacters];
    cell.image.image = [UIImage imageWithData:data];
    cell.titleLabel.text = droneObjDisplay.modelName;
    cell.subTitleLabel.text = droneObjDisplay.modelNumber;
    return cell;
}