我正在制作一个应用程序,其中一个视图有一个表格视图。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;
}
但图像显示不正确。当我上下滚动表格视图时,图像会自动更改。
请查看我的代码并帮助我找到错误。
第二张图片是当我向上和向下滚动表格视图时,第一张图片是在开始时。
请帮我。如果有人知道如何将不同的图像加载到 uitableview 单元格。
提前致谢。