TableView图像加载问题

时间:2011-04-13 04:18:39

标签: iphone objective-c

我正在使用带有单元Controller的自定义tableView。我正在从另一个nib文件加载单元控制器。这是接口代码:

#import <UIKit/UIKit.h>


@interface BeginingCell : UITableViewCell {

    IBOutlet UILabel *SectionTitle;
    IBOutlet UILabel *Option1;
    IBOutlet UILabel *Option2;
    IBOutlet UILabel *Opiton3;
    IBOutlet UILabel *Opiton4;

    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;
    IBOutlet UIButton *button3;
    IBOutlet UIButton *button4;


    IBOutlet UIButton *ansBtn1;
    IBOutlet UIButton *ansBtn2;
    IBOutlet UIButton *ansBtn3;
    IBOutlet UIButton *ansBtn4;


    IBOutlet UIImageView *myImageView; //Here it is


    NSMutableArray *myArray;


}

///Properties
@property(nonatomic,retain)IBOutlet UILabel *SectionTitle;

@property(nonatomic,retain)IBOutlet UILabel *Option1;
@property(nonatomic,retain)IBOutlet UILabel *Option2;
@property(nonatomic,retain)IBOutlet UILabel *Option3;
@property(nonatomic,retain)IBOutlet UILabel *Option4;


@property(nonatomic,retain)IBOutlet UIButton *button1;
@property(nonatomic,retain)IBOutlet UIButton *button2;
@property(nonatomic,retain)IBOutlet UIButton *button3;
@property(nonatomic,retain)IBOutlet UIButton *button4;


@property(nonatomic,retain)IBOutlet UIButton *ansBtn1;
@property(nonatomic,retain)IBOutlet UIButton *ansBtn2;
@property(nonatomic,retain)IBOutlet UIButton *ansBtn3;
@property(nonatomic,retain)IBOutlet UIButton *ansBtn4;

@property(nonatomic,retain)IBOutlet UIImageView *myImageView; //Here it is


@property(nonatomic,retain)NSMutableArray *myArray;


@end




And the implementation is like :

#import "BeginingCell.h"


@implementation BeginingCell

@synthesize SectionTitle,Option1,Option2,Option3,Option4,button1,
button2,button3,button4,myArray,myImageView,ansBtn1,ansBtn2,ansBtn3,ansBtn4;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state.
}


- (void)dealloc {
    [super dealloc];
}


@end

我正在My TableView中加载Cell Controller nib文件。表视图代码如下。但问题是我的图像没有加载。它破碎了。


我合成如下。


@synthesize MyTestArray,listOfItems,MyObject,selectedCountry,QuestionMutableArray,
Manipulatorobj,dictionary,SectionTitle,Option1,Option2,Option3,Option4,ansBtn1,ansBtn2,ansBtn3,ansBtn4;



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    dictionary = [QuestionMutableArray objectAtIndex:0];

    static NSString *CellIdentifier = @"BeginingCell";

    BeginingCell *cell=(BeginingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];

        for(id CurrentObject in topLevelObjects)
        {
            if ([CurrentObject isKindOfClass:[BeginingCell class]]) {

                cell=(BeginingCell *) CurrentObject;
                break;
            }
        }
    }

    // Configure the cell.

    if(indexPath.row==0)
    {

        cell.SectionTitle.text=[dictionary objectForKey:@"question"];
        cell.Option1.text=[dictionary objectForKey:@"option1"];
        cell.Option2.text=[dictionary objectForKey:@"option2"];
        cell.Option3.text=[dictionary objectForKey:@"option3"];
        cell.Option4.text=[dictionary objectForKey:@"option4"];



        [cell.button1 setTitle:@"A." forState:UIControlStateNormal];
        [cell.button2 setTitle:@"B." forState:UIControlStateNormal];
        [cell.button3 setTitle:@"C." forState:UIControlStateNormal];
        [cell.button4 setTitle:@"D." forState:UIControlStateNormal];
        [cell.button4 setTitle:@"D." forState:UIControlStateNormal];

        [MyTestArray release];


    //  [cell.ansBtn1 setTitle:@"A." forState:UIControlStateNormal];

    }

//---Setting the image here---

UIImage *imgDef=[UIImage imageNamed:@"man_kirstie_alley.jpg"]; //It's crushing here
cell.image = imgDef;


    return cell;

}

知道这里出了什么问题吗?

2 个答案:

答案 0 :(得分:0)

我认为那是因为image对象的单元格没有uitableviewcell属性,你的BeginingCell类中也没有UIImage类型的变量名称图像。

所以我认为你要做的就是

cell.imageView.image = [UIImage imageNamed:@"man_kirstie_alley.jpg"];

cell. myImageView.image = [UIImage imageNamed:@"man_kirstie_alley.jpg"];

假设您在此处粘贴代码时没有犯错误

答案 1 :(得分:0)

你的笔尖可能有问题。

创建断点并检查单元格值是否实际具有您合成的属性。

我建议您在子类中有问题地创建视图。而不是使用nib,您还需要在子类的dealloc函数中释放任何保留属性