Ios 将UITableView用作照片流

Ios 将UITableView用作照片流,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试使用UITableView创建照片流(比如Instagram)。我有代码,应该是工作,但图像仍然没有显示。如果我只是创建一个不属于表的UIImageView,则会显示图像,但当我尝试将其添加到表中时,它不会出现。表格正在出现,只是没有图像。这是密码- 正在创建表的视图控制器 #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

我正在尝试使用UITableView创建照片流(比如Instagram)。我有代码,应该是工作,但图像仍然没有显示。如果我只是创建一个不属于表的UIImageView,则会显示图像,但当我尝试将其添加到表中时,它不会出现。表格正在出现,只是没有图像。这是密码-

正在创建表的视图控制器

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //    NSInteger sections = self.objects.count;
    //    if (self.paginationEnabled && sections != 0)
    //        sections++;
    //    return sections;
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PhotoCell";
    PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.thumbImage = [UIImage imageNamed:self.thumbImage];

    if (cell==nil) {
        cell = [[PhotoCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }
    // Configure the cell...

    return cell;
}
单元视图控制器-

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImageView *imageView = [[UIImageView alloc] initWithImage:self.thumbImage];

        imageView.frame = CGRectMake( 0.0f, 0.0f, 320.0f, 320.0f);
        imageView.backgroundColor = [UIColor blackColor];
        imageView.contentMode = UIViewContentModeScaleAspectFit;

        self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.photoButton.frame = CGRectMake( 0.0f, 0.0f, 320.0f, 320.0f);
        self.photoButton.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.photoButton];

        NSLog(@"Working");
        [self.contentView bringSubviewToFront:self.imageView];
    }
    return self; }

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state }

#pragma mark - UIView

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake( 20.0f, 0.0f, 280.0f, 280.0f);
    self.photoButton.frame = CGRectMake( 20.0f, 0.0f, 280.0f, 280.0f); }

@end
我正在使用此选项将在一个视图中捕获的图像发送到另一个视图-

PhotoCell *photoCell = [[PhotoCell alloc] init];
    photoCell.thumbImage = self.thumbImage;

代码的问题是,您试图将图像设置为可能为零的对象

即使
dequeueReusableCellWithIdentifier:
返回有效的单元格实例,您的代码也不会工作,因为您正在
initWithStyle:reuseIdentifier:
方法中添加图像视图,如果单元格不是nil,则不会调用该方法

我建议你做如下的事情

视图控制器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PhotoCell";
PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell==nil) {
    cell = [[PhotoCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

}

[cell setImage:[UIImage imageNamed:self.thumbImage];

return cell;
}
细胞亚类

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Maintain reference to the image view you create.
    self.imageView = [[UIImageView alloc] init];

    self.imageView.frame = CGRectMake( 0.0f, 0.0f, 320.0f, 320.0f);
    self.imageView.backgroundColor = [UIColor blackColor];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.photoButton.frame = CGRectMake( 0.0f, 0.0f, 320.0f, 320.0f);
    self.photoButton.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.photoButton];

    NSLog(@"Working");
    [self.contentView bringSubviewToFront:self.imageView];
}
return self; }

-(void)setImage:(UIImage *)image
{
  [self.imageView setImage:image];
}

同样,如果您正在使用xib/故事板,我建议您在其中创建原型单元。这将是处理这种情况的更简单、更优雅的方法。

除了上面讨论的内容之外,您应该为表视图单元或
光电单元使用异步图像加载。这可以通过
UIImageView+AFNetworking
轻松完成


我知道你想建立一个Instagram照片流。看看这是一个示例代码。您只需将异步映像加载连接到其中即可。

行“[cell setImage:[UIImage ImageName:self.thumbImage]];”会导致两个警告,一个警告说setImage:在IOS 3中不受欢迎,另一个警告说“向thumbImage上的NSString发送UIView的指针类型不兼容@suhas将方法名称更改为setThumbImage而不是setImage。我现在在启动“[PhotoCell setImageView:]:发送到实例0x176a0a10的无法识别的选择器”时出错@suhas您需要将imageView声明为属性。将其添加到单元格子类头文件@property(非原子)UIImageView*imageView中