在我的應用程序中,當提示用戶選擇一個項目時,顯示UITableView。根據商品是否有貨,缺貨,訂購等,我想在UITableViewCell的右側顯示圖像。UIImage作爲UITableViewCell中的附件視圖不顯示
我試過使用附件視圖按鈕,但它沒有顯示在UITableViewCell上。我已經嘗試將UITableViewCellStyle更改爲UITableViewCellStyleValue2(具有細節關閉按鈕的樣式),並且還嘗試直接設置單元的附件類型。
static NSString *CellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
[cell.textLabel setText:@"Testing"];
[cell.detailTextLabel setText:@""];
//cell.accessoryType = UITableViewCellAccessoryNone;
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[myImageFilePath stretchableImageWithLeftCapWidth:0 topCapHeight:1.0]];
[imageView setContentMode: UIViewContentModeScaleAspectFit];
[imageView setFrame:CGRectMake(0, 5, cell.frame.size.height - 10, cell.frame.size.height - 5)];
cell.accessoryView = imageView;
return cell;
當應用程序運行時,UITableViewCells出現的「測試」文字,但沒有圖像上的細胞的右側。
是否有另一種方法,需要重寫爲了實現詳細diclosure按鈕的自定義UIImage?
「按鈕」更多的是一個狀態指示器,因爲我不想輕點按鈕來做與UITableViewCell上其他任何地方不一樣的事情(只需選擇行)。
一些額外的信息:
如果我刪除
[imageView setFrame:CGRectMake(0, 5, cell.frame.size.height - 10, cell.frame.size.height - 5)];
然後將圖像顯示了在UITableViewCell的,但實在是太大了,不適合在同一行上,所以它與其他行的附件視圖重疊。
我正在使用setFrame,以便它將調整圖像以適應UIViewContentModeScaleAspectFit,以便它適合該行,但它似乎阻止圖像出現在所有。
注意,在這個例子cell.frame.size.height = 44
如果我改變幀:
[imageView setFrame:CGRectMake(0.0, 0.0, 120, 44)];
然後將圖像顯示在該行。
但是,由於它的寬度是120像素,因此它將切斷文本,並在之前需要的時候拖尾。該圖像可以適合大約3倍的前後...,寬度爲120,高度爲44.
如何獲得尾部...以更接近圖像?
如果我將120降低到較低值,圖像將向右移動並最終從單元移開。
我試着將X設置爲cell.frame.size.width-44,但它仍然不可見。 – jkh