我正在制作一个应用程序,其中一个视图有一个tableview。 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单元格。
提前感谢。
答案 0 :(得分:2)
试试这个:
- (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;
}
答案 1 :(得分:1)
首先,如果您打算添加子视图,则应该使用UITableViewCell
的子类(有关详细信息,请参阅Apple's documentation)。但是,我离题了......
有一种更简单的方法可以向您的单元格添加图像(使用UITableViewCellStyleDefault
):
if (!cell)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if ((que.length!=0)&&(ans.length!=0)) {
cell.imageView.image = [UIImage imageNamed:@"ques.png"];
cell.imageView.frame = CGRectMake(0, 0, 320, 75);
...
}
else {
cell.imageView.image = [UIImage imageNamed:@"answ.png"];
cell.imageView.frame = CGRectMake(0, 0, 320, 40);
...
}
答案 2 :(得分:0)
试试这个
- (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;
}
我在这里做的是将你的图像改变条件置于
之外if(cell == nil){ } statement
答案 3 :(得分:0)
尝试这个,你必须在创建电话之前放置}
- (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;
}
希望这可以帮助你..