2011-09-19 56 views
0

我在我的UITableView中的自定義單元格中有不同的文本,每個文本都有一個類別。根據類別一定的圖像被分配到的UIImageView在我CustomCell這樣的: - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {動態分配圖像到CustomCell

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 ([email protected]"Стихи") 
{ 
    NSLog(@"Assigning proper image!!"); 

     cell.iconImage.image=[UIImage imageNamed:@"stixi.png"]; 
} 
if ([email protected]"Анекдоты") 
{ 
    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; 

} 仍然不工作......任何想法?謝謝!

回答

3

問題是你的字符串相等檢查...這個categoryForPicture == @「Анекдоты」只有在兩個是相同的對象時纔會返回true,在這裏永遠不會出現這種情況,你想給我們NSString isEqualToString:in爲了實現自己的目標......所以

[categoryForPicture isEqualToString:@""] 

希望幫助

+0

和請不要使用imageNamed:代替用戶imageWithContentsOfFile:避免圖像緩存 - 這樣做可以節省您的運行時內存.. !! – samfisher