我有一个自定义单元格,上面有不同的子视图。其中一个是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];
}
}
}
但这不起作用,因为现在我在每个细胞上得到相同的图像......
修改
我忘了说我每节只使用一行只有一行,这就是我使用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];
}
}
答案 0 :(得分:3)
尝试这种方法 - 在故事板中添加UIImageView,只要您需要app逻辑,就可以隐藏和取消隐藏。
- (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;
}
答案 1 :(得分:1)
请使用这个:
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
您需要使用indexPath.row
代替indexPath.section
答案 2 :(得分:1)
您正在为部分添加图片。所以,请执行以下操作:
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];
}
答案 3 :(得分:1)
通过这种方式尝试,如果条件如下,请不要检查里面的图像是否有希望这有助于你,将其改为你的要求
- (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;
}
答案 4 :(得分:0)
- (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];
}
}
}