2010-05-27 91 views
1

我是Iphone應用程序開發和目標C的新手。我想知道是否有方法在UITableViewCell中插入圖片 - 無論是否創建自定義單元格?如何在UITableViewCell中添加圖片

我想將它定位在「第二行」,就像「cell.detailTextLabel.text」出現一樣。

有什麼建議嗎?提前致謝。

回答

0

自定義單元格是執行此操作的標準方法。網上有很多教程,比如this one

0

在你的餐桌控制器:

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

    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithFrame:CGZeroRect]; 

    CGRect cellFrame = cell.frame; 
    NSInteger offetXInCell = 3, offsetYInCell = 7;   

    cellFrame.origin.x += offsetXInCell; 
    cellFrame.origin.y += offsetYInCell; 

    [cell setFrame:cellFrame]; 

    UIImage *newImage = [UIImage imageNamed:@"MyPicture"]; 
    cell.image = newImage; 

    // .... other initializations of cell 
    return cell; 
} 

我不知道是否會工作。你應該把你的數字放在偏移量上。

0

檢查的tableview套件中的示例代碼

1

你可以簡單地用你的圖片作爲小區的子視圖添加的UIImageView。它將以兩種方式工作 - 無需創建自定義單元格。

下面是一些示例代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *kCellID = [NSString stringWithFormat:@"%d", indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease]; 

    UIImageView *MyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100.0, 33.0, 300.0, 110.0)]; //put your size and coordinates here 

    MyImageView.image = [UIImage imageNamed:@"MyImage.png"]; 
    [cell.contentView addSubview: MyImageView]; 
    [MyImageView release]; 

    return cell; 
}