动态地将图像分配给CustomCell

时间:2011-09-19 15:33:00

标签: iphone objective-c uitableview

我的UITableView中的自定义单元格中有不同的文本,每个文本都有一个类别。根据类别,在我的CustomCell中将某个图像分配给UIImageView,如下所示: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"CCell";

// Dequeue or create a cell of the appropriate type.
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
   cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   cell.accessoryType = UITableViewCellAccessoryNone;


}

// Configure the cell.
cell.primaryLabel.text = [self.arrayWithTextsFromSelectedCategory objectAtIndex: indexPath.row];
NSString *keyForText=[self.arrayWithTextsFromSelectedCategory objectAtIndex:indexPath.row];
categoryForPicture=[dicWithTitlesAsKeysAndCategoriesAsValues objectForKey:keyForText];

//cell.categoryImg=[dicWithTitlesAsKeysAndCategoriesAsValues objectForKey:keyForText];
NSLog(@"Current category!!! %@", categoryForPicture);
NSLog(@" %@ ", keyForText);
//NSString *textToExtract;
//textToExtract=[self.texts objectForKey:keyForText];

cell.secondaryLabel.text=[self.texts objectForKey:keyForText];
if (categoryForPicture==@"Стихи") 
{
    NSLog(@"Assigning proper image!!");

        cell.iconImage.image=[UIImage imageNamed:@"stixi.png"];
}
if (categoryForPicture==@"Анекдоты") 
{
    NSLog(@"Assigning proper image!!");

    cell.iconImage.image=[UIImage imageNamed:@"jokes.png"];
}
else 
{

        cell.iconImage.image=[UIImage imageNamed:@"stand_icon.png"];
}



//cell.imageView.image=[UIImage imageNamed:@"cell.png"];

//cell.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell.png"]]];
return cell;

} 还是不行......有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:3)

问题是你的字符串相等检查...这个categoryForPicture == @“Анекдоты”只有当两个是同一个对象时才会返回true,这里永远不会出现这种情况,你想要我们NSStrings isEqualToString:顺序实现你的目标......所以

[categoryForPicture isEqualToString:@""]

希望有所帮助