0

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

// 实现 viewDidLoad 以在加载视图后进行额外的设置,通常来自 nib。- (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;
    }
4

3 回答 3

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;

这是最好的方法

于 2012-10-30T13:03:22.343 回答
0

假设一般节是第0节(即第一节)

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

   }
于 2011-04-08T18:18:19.957 回答
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;
}
于 2011-04-08T18:18:57.503 回答