gpt4 book ai didi

ios - UITableViewCell ios 的错误

转载 作者:行者123 更新时间:2023-11-29 00:45:32 24 4
gpt4 key购买 nike

我正在处理一个 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];
}

有什么解决办法吗?!

最佳答案

您的单元格样式是 UITableViewCellStyleSubtitle 并且您还手动添加了标题和副标题标签。

如果你使用单元格的样式作为 UITableViewCellStyleSubtitle 那么你可以直接使用

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

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

关于ios - UITableViewCell ios 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38651026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com