gpt4 book ai didi

ios - 删除 subview 并将其添加到自定义 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 18:20:13 25 4
gpt4 key购买 nike

我有一个带有不同 subview 的自定义单元格。其中之一,UIImageView,是可选的。因此可能存在单元格中不需要图像的情况。我的问题是我不知道如何删除不需要它的单元格的图像,以及如何为需要它的单元格添加图像?我知道我只应该像这样添加和删除 subview :

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

static NSString *CellIdentifier1 = @"NewsCell";

GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

if(newsCell == nil){

newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];

//Here i get the Image from my Array (self.newsList)
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];

//Here im checkin if an image exists
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){

//If no Image exists remove the subview
[newsCell.boardNoteImage removeFromSuperview];

} else {

//If an image exists, check if the subview is already added
if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
[newsCell.contentView addSubview:[newsCell.boardNoteImage];

}
}
}

但这不起作用,因为现在我在每个单元格上得到相同的图像......

编辑:

对不起,我忘了说我使用的 Section 每个部分只有一行,这就是我使用 indexPath.section 的原因!

我从网络服务器获取所有数据,并将所有内容放入我的数组 self.newsList 中!

编辑 2:使用此代码,我获得了正确单元格的正确图像,但是向下滚动并向上滚动后,一切都消失了:/

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

static NSString *CellIdentifier1 = @"NewsCell";

GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

if(newsCell == nil){

newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];

}

NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];

if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){

[newsCell.boardNoteImage removeFromSuperview];
newsCell.boardNoteImage.image = nil;
} else {

if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {

newsCell.boardNoteImage.frame = CGRectMake(newsCell.boardNoteImage.frame.origin.x, newsCell.boardNoteImage.frame.origin.y, newsCell.boardNoteImage.frame.size.width, newsCell.boardNoteImage.frame.size.height);

[newsCell.contentView addSubview:newsCell.boardNoteImage];

}
}

最佳答案

尝试这种方法 - 在 Storyboard中添加 UIImageView,您可以在需要应用逻辑时隐藏和取消隐藏。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SongCell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SongCell"];
//dont check the presence of image in this part becz it is called during creation only not on resuing
}
//check if image is present or not
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
if(boardImg)
{
//image is present
cell.boardNoteImage.hidden = NO;

cell.boardNoteImage.image = boardImg;


}
else
{
cell.boardNoteImage.frame = CGRectZero;//which places a zero rect means no image
cell.boardNoteImage.hidden = YES;

}

return cell;
}

关于ios - 删除 subview 并将其添加到自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881218/

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