无法在tableview中查看图像

时间:2013-11-04 15:29:49

标签: ios uitableview segue

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
    cell.textLabel.text = [temp objectForKey:@"name"];
    cell.detailTextLabel.text = [[temp objectForKey:@"distance"] stringByAppendingString:@" km from Banglore"];
    cell.imageView.image =[UIImage imageNamed:[temp objectForKey:@"category"]];
    return cell;
}

2 个答案:

答案 0 :(得分:4)

将单元格的样式更改为UITableViewCellStyleDefault

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

如果您想在单元格中添加两个标签,请将其添加到单元格的contentView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }



       // Configure the cell...
        NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
        cell.textLabel.text = [temp objectForKey:@"name"];
    UIlabel *detailLbl = [[UILabel alloc]initWithFrame:CGRectMake(60,10,200,50)];
    [detailLbl setText:[[temp objectForKey:@"distance"] stringByAppendingString:@" km from Banglore"];];
[cell.contentView addSubView:detailLbl];
        cell.imageView.image =[UIImage imageNamed:[temp objectForKey:@"category"]];
        return cell;
    }

答案 1 :(得分:1)

试试此代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {
                static NSString *CellIdentifier = @"Cell";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                }

             // Configure the cell...
                NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
                cell.textLabel.text = [temp objectForKey:@"name"];
                cell.detailTextLabel.text = [[temp objectForKey:@"distance"] stringByAppendingString:@" km from Banglore"];
                //cell.imageView.image =[UIImage imageNamed:[temp objectForKey:@"category"]];

        //write this code 

         // Load the image with an GCD block executed in another thread
            dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
            dispatch_async(downloadQueue, ^{

                UIImage * image = [UIImage imageNamed:[temp objectForKey:@"category"]];

                dispatch_async(dispatch_get_main_queue(), ^{

                     cell.imageView.image = image;
                     cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
                     [cell setNeedsLayout];
                });
            });
            dispatch_release(downloadQueue);

        return cell;

            }