gpt4 book ai didi

iphone - 将不同的图像设置到 uitableview 单元格

转载 作者:行者123 更新时间:2023-12-03 20:51:38 26 4
gpt4 key购买 nike

我正在制作一个应用程序,其中一个 View 有一个表格 View 。 Tableview 单元格有两个条件。根据条件,将在 uitableview 单元格上设置两个图像,即

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *que =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"question"];
NSString *ans =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"answer"];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.imageView.image=nil;

if ((que.length!=0)&&(ans.length!=0)) {
UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 75)];
imag.image = [UIImage imageNamed:@"ques.png"];
[cell.contentView addSubview:imag];
questext = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
questext.backgroundColor = [UIColor clearColor];
questext.delegate = self;
questext.tag = 101;
questext.textAlignment = UITextAlignmentLeft;
questext.editable = NO;
questext.scrollEnabled = YES;
[cell addSubview:questext];

anstext = [[UITextView alloc]initWithFrame:CGRectMake(10, 37, 300, 35)];
anstext.backgroundColor = [UIColor clearColor];
anstext.delegate = self;
anstext.tag = 102;
anstext.scrollEnabled = YES;
anstext.textAlignment = UITextAlignmentLeft;
anstext.editable = NO;
[cell addSubview:anstext];
}
else {
UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
imag.image = [UIImage imageNamed:@"answ.png"];
[cell.contentView addSubview:imag];

onlyques = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
onlyques.backgroundColor= [UIColor clearColor];
[onlyques setScrollEnabled:YES];
onlyques.delegate = self;
onlyques.tag = 103;
onlyques.textAlignment = UITextAlignmentLeft;
onlyques.editable = NO;
onlyques.scrollEnabled = YES;
[cell addSubview:onlyques];
}
}
questext = (UITextView*)[cell viewWithTag:101];
questext.text = que;

anstext = (UITextView*)[cell viewWithTag:102];
anstext.text = ans;

onlyques = (UITextView*)[cell viewWithTag:103];
onlyques.text = que;

return cell;
}

但是图像显示不正确。当我上下滚动表格 View 时,图像会自动更改。

请查看我的代码并帮助我找到错误。 enter image description here

enter image description here

第二张图片是当我上下滚动表格 View 时,第一张图片位于开头。

请帮助我。如果有人知道如何将不同的图像加载到 uitableview 单元格。

提前致谢。

最佳答案

试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *que =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"question"];
NSString *ans =[[userqueries objectAtIndex:indexPath.row]objectForKey:@"answer"];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.imageView.image=nil;
}
else
{
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

if ((que.length!=0)&&(ans.length!=0))
{
UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 75)];
imag.image = [UIImage imageNamed:@"ques.png"];
[cell.contentView addSubview:imag];
questext = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
questext.backgroundColor = [UIColor clearColor];
questext.delegate = self;
questext.tag = 101;
questext.textAlignment = UITextAlignmentLeft;
questext.editable = NO;
questext.scrollEnabled = YES;
[cell addSubview:questext];

anstext = [[UITextView alloc]initWithFrame:CGRectMake(10, 37, 300, 35)];
anstext.backgroundColor = [UIColor clearColor];
anstext.delegate = self;
anstext.tag = 102;
anstext.scrollEnabled = YES;
anstext.textAlignment = UITextAlignmentLeft;
anstext.editable = NO;
[cell addSubview:anstext];
}
else
{
UIImageView* imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
imag.image = [UIImage imageNamed:@"answ.png"];
[cell.contentView addSubview:imag];

onlyques = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 35)];
onlyques.backgroundColor= [UIColor clearColor];
[onlyques setScrollEnabled:YES];
onlyques.delegate = self;
onlyques.tag = 103;
onlyques.textAlignment = UITextAlignmentLeft;
onlyques.editable = NO;
onlyques.scrollEnabled = YES;
[cell addSubview:onlyques];
}

questext = (UITextView*)[cell viewWithTag:101];
questext.text = que;

anstext = (UITextView*)[cell viewWithTag:102];
anstext.text = ans;

onlyques = (UITextView*)[cell viewWithTag:103];
onlyques.text = que;

return cell;
}

关于iphone - 将不同的图像设置到 uitableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13818631/

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