0

I am trying to create a photo feed using a UITableView. I have the code to create the table and, when a photo is taken, display it in the feed but the image isn't displaying. The image is defiantly being captured and if I use a UIImageView to display it, it works, but when I try to add it to the table, it doesn't. I have also noticed that when I click on a row of the empty table, my didSelectRow action isn't working so I'm not sure if the problem is with the table or the way the image is being displayed.

Here is the code I am using to create the table:

tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorColor = [UIColor clearColor];
[self.view addSubview:tableView];
[_tableView release];
[self.tableView release];

Here is the rest of the code for the table:

#pragma mark - UITableViewDataSource
// number of section(s), now I assume there is only 1 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}

// number of row in the section, I assume there is only 1 row
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    if (indexPath.section >= self.objects.count) {
    //        // Load More Section
    //        return 320.0f;
    //    }
    //
    return 320.0f;
}

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"HistoryCell";
    // Similar to UITableViewCell, but
    PhotoCell *cell = (PhotoCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    // Just want to test, so I hardcode the data
    cell.descriptionLabel.text = @"Testing";
    cell.thumbImage = self.thumbImage;
    //cell.backgroundView = self.thumbImage;

    return cell;
}

#pragma mark - UITableViewDelegate
// when user tap the row, what action you want to perform
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailsViewController = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailsViewController animated:YES];
}

Here is the PhotoCell.M:

 #import "PhotoCell.h"
#import "FeedViewController.h"
#import "DetailViewController.h"

@implementation PhotoCell

@synthesize thumbImage = _thumbImage;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // configure control(s)

        UIImageView *testView = [[UIImageView alloc] initWithImage:self.thumbImage];
        testView.frame = CGRectMake(0, 0, 320, 320);
        [self addSubview:testView];
        [self bringSubviewToFront:testView];

    }
    return self;
}

@end
4

3 回答 3

1

这只是一个快速的(我没有测试过这是 Xcode)

尝试这个:

#import "PhotoCell.h"
#import "FeedViewController.h"
#import "DetailViewController.h"

@interface PhotoCell ()
@property(nonatomic, strong) UIImageView testView;
@end

@implementation PhotoCell

@synthesize thumbImage = _thumbImage;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // configure control(s)

        self.testView = [[UIImageView alloc] initWithFrame:CGRectZero]
        [self.contentView addSubview:self.testView];

    }
    return self;
}

- (void) layoutSubviews {
    [super layoutSubviews];
    self.testView.frame = CGRectMake(0, 0, 320, 320);
}

- (void) setThumbImage:(UIImage *)image{
    _thumbImage = image;
    self.testView.image = _thumbImage
}
@end

可以,然后呢:

我已从该init方法中删除了框架信息,因为此时单元格不知道将其移入的框架layoutSubviews对于您要添加更复杂布局的时间更安全(即使您在此单元格中拥有的只是图像)

我重写了默认设置器,self.thumbImage以便我们不存储 UIImage 对象,而是告诉 imageView (testView) 图像是什么,并且应该显示它。

将单元格的所有子视图添加到其中也是一个好习惯,self.contentView因为self苹果对其单元格状态做了很多自动魔术,并且将子视图添加到 self 可能会导致例如编辑模式下的不良行为。

希望能帮助到你。

于 2013-11-14T04:38:30.573 回答
0

我认为这是因为在 PhotoCell.M 中的“initWithStyle”时尚未设置 thumbImage 属性。

如果是我,我会覆盖 PhotoCell 的 setThumbImage 函数以将图像设置为 imageview。

于 2013-11-14T02:09:44.153 回答
0

当 testView 为 alloc 时,此时 self.thumbImage 为 nil。

你可以简单地设置 cell.imageView.image 来试一试!

于 2013-11-14T02:16:25.437 回答