[英]Remove and add Subview to Custom UITableViewCell
我有一个带有不同子视图的自定义单元格。 其中之一,UIImageView是可选的。 因此可能会出现单元格中不需要图像的情况。 我的问题是我不知道如何删除不需要它的单元格的图像,以及如何为需要它的单元格添加图像? 我知道我只应该这样添加和删除子视图:
- (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!
EDIT2:使用此代码,我可以获得正确的单元格的正确图像,但是向下滚动并向上备份之后,一切都消失了:/
- (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];
}
}
尝试这种方法-在情节提要中添加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;
}
请使用这个:
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
您需要使用indexPath.row
而不是indexPath.section
您正在添加图像的部分。 因此,请执行以下操作:
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
//If no Image exists remove the subview
[newsCell.boardNoteImage removeFromSuperview];
newsCell.boardNoteImage = nil;
} else {
//set the frames here if required
[newsCell addSubview:boardNoteImage];
}
编辑1:
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 = [[UIImageView alloc] init];
// SET THE IMAGE HERE
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];
}
尝试这种方式,如果情况如下所示,请不要检查内部图像是否存在,希望对您有所帮助,将其更改为您的要求
- (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.image = boardImg;
}
else
{
cell.boardNoteImage.frame = CGRectZero;//which places a zero rect means no image
cell.boardNoteImage. hidden = YES;//place a nil in the image
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//static NSString *CellIdentifier1 = @"NewsCell";
NSString *CellIdentifier1 = [NSString stringWithFormat:@"cell %d",indexPath.row];
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
newsCell = nil;
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];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.