提问人:Six 提问时间:12/9/2022 最后编辑:AdriaanSix 更新时间:12/9/2022 访问量:60
如何在customcell objective-c中以编程方式正确添加图像
How to properly add an image programmatically in a customcell objective-c
问:
我正在尝试以编程方式在自定义中添加图像,但它显示如下:tableviewcell
我想要这样:
(如通知)
这是我的代码:
ViewController.m的
@implementation albumsSingerVC
- (void)viewDidLoad{
_matableview.delegate = self;
_matableview.dataSource = self;
}
- ( nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"customCell";
customCellSingersAlbumVC *cell = (customCellSingersAlbumVC*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
[tableView registerNib:[UINib nibWithNibName:@"customCellSingersAlbumView" bundle:nil] forCellReuseIdentifier:MyIdentifier];
}
cell = (customCellSingersAlbumVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
UIImageView *imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:@"pink.png"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); //I also
changed the values to a small ones.
[[cell imageView ] addSubview:imageview];
return cell;
}
@end
我用不同的语法尝试了它。
我也试着做不同的事情:
customCell.m:
@implementation customCellSingersAlbumVC
- (void)viewDidLoad{
UIImageView *imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:@"pink.png"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150);
[self.contentView addSubview:imageview];
}
@end
但从未被触发。viewdidload
答:
-1赞
Six
12/9/2022
#1
嗨,我终于找到了如何做到这一点,如果有人感兴趣,这里是代码:
- ( nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"customCell";
customCellSingersAlbumVC *cell = (customCellSingersAlbumVC*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
[tableView registerNib:[UINib nibWithNibName:@"customCellSingersAlbumView" bundle:nil] forCellReuseIdentifier:MyIdentifier];
}
cell = (customCellSingersAlbumVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// Create a new UIImage object
UIImage *image = [UIImage imageNamed:@"pink.png"];
// Create a new image view
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Set the image view's frame to the desired size
imageView.frame = CGRectMake(95, 10, 30, 30);
// Create a new label object
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
// Set the label's text
label.text = @" 6";
// imageView.tintColor = UIColor.blueColor;
imageView.layer.cornerRadius = imageView.frame.size.width /2;
imageView.layer.masksToBounds = YES;
imageView.layer.borderWidth = 0;
// Add the image view to your table view cell's content view
[imageView addSubview:label];
[cell.contentView addSubview:imageView];
return cell;
}
评论
2赞
HangarRash
12/10/2022
仅代码答案并不像它们可能的那样有用。如果你的答案清楚地解释了问题所在以及答案中的代码如何解决问题,那将是最好的。
2赞
DonMag
12/10/2022
不应在 中添加子视图。单元格是重复使用的,因此当您滚动时,您将添加越来越多的重复子视图。相反,您应该使用已经存在的元素来设计单元格,然后根据需要在 中显示或隐藏它们。cellForRowAtIndexPath
cellForRowAtIndexPath
评论