2011-04-08 108 views
0

我想在表格单元格中显示除图像以外的图像。我有2个部分,一般和作业。现在我只想在旁边显示图像,除了常规部分外没有图像。下面的代码显示“作业”部分的图像,然后再次重复“常规”部分的图像。任何人都可以帮我解决这个问题吗?将图像添加到部分中的表格单元格

//在加载视图后,通常从一个笔尖实现viewDidLoad以执行其他设置。 - (无效)viewDidLoad中{

staffImages = [[NSMutableArray alloc] init]; 

    NSArray *arrTemp1 = [[NSArray alloc] initWithObjects:@"TRANSPORTATION",@"ROOMS",@"ACTIVITIES",nil]; 
    NSArray *arrTemp2 = [[NSArray alloc] initWithObjects:@"CHAT",@"SEARCH",@"CALL",@"MORE",nil]; 

    NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:arrTemp1,@"Assignments",arrTemp2, 
                        @"General",nil]; 

    //Add item images 
    [staffImages addObject:@"transportation.png"]; 
    [staffImages addObject:@"hotel.png"]; 
    [staffImages addObject:@"activities.png"]; 


    //Set the title 
    self.navigationItem.title = @"STAFF ASSIGNMENTS"; 
    self.tableContents=temp; 
    [temp release]; 

    self.sortedKeys =[self.tableContents allKeys]; 
    [arrTemp1 release]; 
    [arrTemp2 release]; 

    [super viewDidLoad]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]]; 

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 
    if(cell == nil) { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
         reuseIdentifier:SimpleTableIdentifier] autorelease]; 

     } 
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 

    cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]]; 

    return cell; 
    } 

回答

0

假设一般截面是段0(即第一部分)

if (indexPath.section == 1) { 
    cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]]; 

    } 
+0

谢谢,这就像一个魅力.. – appuser 2011-04-08 18:30:57

+0

伟大的工作!祝你好运! ;) – Jordan 2011-04-08 18:42:22

0

您需要检查indexPath的部分。您可能还需要使用部分检查以确保您将单元格的文本设置为正确的值。

//You do not need to actually define this value 
//this is just for naming purposes 
#define SECTION_ASSIGNMENTS 0 

... 

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]]; 

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 
    if(cell == nil) { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
         reuseIdentifier:SimpleTableIdentifier] autorelease]; 

     } 
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 
    if(indexPath.section == SECTION_ASSIGNMENTS) 
    { 
     cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]]; 
    } 
    return cell; 
} 
1
NSMutableData *data=[NSMutableData dataWithContentsOfURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@",[[dataArray objectAtIndex:indexPath.row]objectForKey:@"LINK"]]]]; 

//NSLog(@"%@",data); 

UIImage *img=[UIImage imageWithData:data]; 

cell.imageView.image=img; 

这是最好的办法

相关问题