set different images to uitableview cell
Posted
by
Sudha
on Stack Overflow
See other posts from Stack Overflow
or by Sudha
Published on 2012-12-11T10:47:25Z
Indexed on
2012/12/11
11:03 UTC
Read the original article
Hit count: 258
I am making an app in which one of the view has a tableview. Tableview cell has two conditions. There are two images which are going to be set on uitableview cell according to the condition i.e.
- (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;
}
But image is not appearing properly. As I scroll up and down the table view ,images get changes automatically.
Please look upon my code and help me in finding the error.
second image is when I scroll up and down the table view and first image is in the starting.
please help me. if any one knows how to load different images to uitableview cell.
thanks in advance.
© Stack Overflow or respective owner