2014-03-12 49 views
0

嗨,我需要以編程方式在UITableViewCells中添加不同的圖像。我該怎麼做,我正在嘗試一些代碼。但圖像不顯示在UITableViewCells。 這是我的代碼如下。請幫助任何人。提前致謝。如何在UITableviewcells中以編程方式添加圖像

-(void)viewDidLoad 
{ 
    arrImages=[[NSMutableArray alloc]initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil]; 
} 

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

    static NSString *[email protected]"cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (cell == nil) 

    { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]; 

    } 

    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; 

    cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]]; 

    return cell; 

} 
+0

請顯示您的實際代碼。這甚至不會編譯。 – jrturton

+0

請檢查圖像localtion。轉到項目構建階段 - >複製包資源添加應用程序包中的所有圖像。 – Musthafa

+0

請幫助我的任何機構.... – user3222991

回答

1

在您的.h文件在您.m文件創建的NSArray的屬性

@property NSArray *imgArray; 

合成

@synthesize imgArray; 

寫這在您的viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    // Initialize images... 
    imgArray = [NSArray arrayWithObjects:@"yourFirstImageName.png", @"yourSecondImageName.png", @"yourThirdImageName.png",....., @"yourSeventhImageName.png", nil]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 


     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; // your cell's height should be greater than 48 for this. 
     imgView.tag = 1; 
     [cell.contentView addSubview:imgView]; 
     imgView = nil; 

     UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)]; 

     lbl.backgroundColor = [UIColor clearColor]; 
     [lbl setTag:2]; 
     [cell.contentView addSubview:lbl]; 
     lblDisch = nil; 
    } 

    UIImageView *_imgView = (UIImageView *)[cell.contentView viewWithTag:1]; 
    _imgView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexPath.row]]; 

    UILabel *_lbl = (UILabel *)[cell.contentView viewWithTag:2]; 
    _lbl.text = [arrFont objectAtIndex:indexPath.row]; // arrFont is your array 
    return cell; 
} 

希望這會幫助你。

+0

感謝您的回覆。但我有七張圖片,我可以這樣做。 – user3222991

+0

用不同的標籤製作七個imageview對象。 –

+0

,謝謝你的repsonse.But我不明白你的帖子。你的意思是創建七個UIImageviews並分配相應的圖像? – user3222991

2

這就是你可能想:

- (void)viewDidLoad 
{ 
    self.arrImages = [[NSMutableArray alloc] initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil]; 
} 

- (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] 
    } 

    cell.imageView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]]; 

    return cell; 
} 

附::下次請花些時間格式化源代碼,解釋什麼是錯的,你嘗試了什麼,你想完成什麼。謝謝

+0

感謝您的response.I創建UIImageveiw programatically.so如何編寫code.please一旦檢查我的帖子 – user3222991

+0

如果你想自定義UITableViewCell的imageView屬性的外觀,那麼你應該真的爲自定義的UITableViewCell子類添加自定義的UIImageView屬性*作爲子視圖。您目前正在使用默認的「UITableViewCell」的''imageView''屬性,該屬性爲**只讀**。 [查看更多內容](https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#jumpTo_13)。 – damirstuhec

-2

在你的.h文件中創建一個@property的NSMutableArray。

NSMutableArray *data; 

寫下此代碼您的.m文件。

- (void)viewDidLoad 
{ 
    data = [[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",nil]; 
    [super viewDidLoad]; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [data count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]]; 

    return cell; 
} 
相關問題