【问题标题】:Dynamically assigning image to CustomCell将图像动态分配给 CustomCell
【发布时间】:2011-09-19 15:33:00
【问题描述】:

我的 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;

} 仍然不起作用...有什么想法吗?谢谢!

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

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

    [categoryForPicture isEqualToString:@""]
    

    希望对你有帮助

    【讨论】:

    • 请不要使用 imageNamed: 代替用户 imageWithContentsOfFile: 以避免图像缓存 - 这样做会节省您的运行时内存..!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2018-12-08
    • 2016-07-14
    相关资源
    最近更新 更多