gpt4 book ai didi

ios - 按需显示/隐藏自定义单元格中的按钮不起作用

转载 作者:行者123 更新时间:2023-11-29 02:48:40 25 4
gpt4 key购买 nike

我试图在我的表格 View 中的某些单元格上显示一个按钮,具体取决于该单元格中显示的图像类型

所以我有两个 NSMutableArray,一个保存缩略图 URL,另一个 NSMutableArray 保存 url 的类型,如果它是图像或视频的话

问题是按钮没有显示在所有视频类型单元格上

这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TVCustomCell";

TVCustom *cell = (TVCustom *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TVCustom" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[TVCustom class]]) {
cell = (TVCustom *) currentObject;
break;
}
}
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSString *imgURL = [arrayImagesURL objectAtIndex:indexPath.row];
NSURL *imageURL = [NSURL URLWithString:imgURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
cell.thumbImg.image = image;
});
});

NSString *MediaType = [NSString stringWithFormat:@"%@", [arrayType objectAtIndex:indexPath.row]];

if ([MediaType isEqualToString:@"video"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(120, 319, 50, 30);
[button setTitle:@"Play" forState:UIControlStateNormal];
button.tag = indexPath.row;
[button addTarget:self action:@selector(PlayBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];
}

return cell;
}

我不知道我做错了什么,知道吗?

最佳答案

我在这里看到了不同的问题。

首先

每次用户在 tableView 中滚动时,您的代码都会创建新按钮。这是因为您使用了很棒的 dequeueReusableCellWithIdentifier 并返回现有单元而不是创建新单元。

但是然后你在已经存在的单元格上添加一个新按钮(可能有也可能没有按钮)

[cell.contentView addSubview:button];

我建议您在 TVCustom 类中添加按钮:

在 TVCustom.h 中

@property (strong, nonatomic) UIButton *viedoButton;

在 TVCustom.m 中

- (UIButton *)videoButton
{
if (!_videoButton) {
_videoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_videoButton.frame = CGRectMake(120, 319, 50, 30);
[_videoButton setTitle:@"Play" forState:UIControlStateNormal];
[_videoButton addTarget:self action:@selector(PlayBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
_videoButton.backgroundColor= [UIColor clearColor];
[self.contentView _videoButton];
}
return _videoButton;
}

然后在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 你只需要做:

if ([MediaType isEqualToString:@"video"]) {
cell.videoButton.hidden = NO;
} else {
cell.videoButton.hidden = YES;
}

这样一来,每个单元格将只有一个按钮,并且仅在您需要时才可见,而不是每次都重新分配。但是 PlayBtnClicked 必须在您的 TVCustom 类中并且通过委托(delegate)调用您的 viewController

第二

您不应在 cellForRowAtIndexPath 方法中处理图像加载。将它封装到您的 TVCustom 类中:

在 TVCustom.h 中

@property (strong, nonatomic) NSString *imgURL;

在 TVCustom.m 中

- (void)setImgURL:(NSString *)imgURL
{
_imgURL = imgURL;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSURL *imageURL = [NSURL URLWithString:imgURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
self.thumbImg.image = image;
});
});
}

然后在 cellForRowAtIndexPath 中它看起来像:

cell.imgURL = [arrayImagesURL objectAtIndex:indexPath.row];

第三

我不知道你是否有任何特殊原因:

if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TVCustom" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[TVCustom class]]) {
cell = (TVCustom *) currentObject;
break;
}
}
}

但一个好的方法是在您的 viewController 的 viewDidLoad(或其他方法)中:

NSString *identifier = @"TVCustomCell";
NSString *nibName = @"TVCustom";
UINib *cellNib = [UINib nibWithName:nibName];
[self.tableView registerNib:cellNib forCellReuseIdentifier:identifier];

您的最终代码应如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"TVCustomCell";

TVCustom *cell = (TVCustom *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.imgURL = [arrayImagesURL objectAtIndex:indexPath.row];

if ([MediaType isEqualToString:@"video"]) {
cell.videoButton.hidden = NO;
} else {
cell.videoButton.hidden = YES;
}

return cell;
}

关于ios - 按需显示/隐藏自定义单元格中的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24799134/

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