将图像添加到表格单元格中

时间:2011-04-08 18:14:02

标签: iphone ios4 uitableview grouped-table

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

//在加载视图后,通常从nib实现viewDidLoad以进行其他设置。      - (void)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;
    }

3 个答案:

答案 0 :(得分: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;

这是最好的方法

答案 1 :(得分:0)

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

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

   }

答案 2 :(得分: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;
}
相关问题