2008-12-31 32 views

回答

113

您需要創建自定義視圖並將其分配給UITableViewCell對象的accessoryView屬性。喜歡的東西:

myCell.accessoryView = [[ UIImageView alloc ] 
         initWithImage:[UIImage imageNamed:@"Something" ]]; 
+7

如果我這樣做,我注意到的UITableViewDelegate方法 - [的tableView:accessoryButtonTappedForRowWithIndexPath:]不再被調用。那是預期的行爲?有沒有辦法保持這種方法的工作? – 2010-01-05 01:54:47

+0

還有一些更多的解釋在http://stackoverflow.com/questions/869421/using-a-custom-image-for-a-uitableviewcells-accessoryview-and-having-it-respond – jarnoan 2010-10-06 13:21:45

+0

是否有可能使用UIApperance UITableViewCell上的代理? – Claus 2013-07-03 10:37:11

7

我遇到同樣的問題,因爲格雷格 - 附屬視圖不跟蹤(如果你使用的UIImageView)

我解決了它這樣的:

UIImage * image = [ UIImage imageNamed:@"disclosure-button-grey.png" ] ; 
UIControl * c = [ [ UIControl alloc ] initWithFrame:(CGRect){ CGPointZero, image.size } ] ; 

c.layer.contents = (id)image.CGImage ; 
[ c addTarget:self action:@selector(accessoryTapped:) forControlEvents:UIControlEventTouchUpInside ] ; 
cell.accessoryView = c ; 
[ c release ] ; 
9
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UIImage *indicatorImage = [UIImage imageNamed:@"Arrow.png"]; 
    cell.accessoryView =[[[UIImageView alloc] initWithImage:indicatorImage] autorelease]; 


    return cell; 
} 

以及我做到這一點上面

-2

注意給予幫助代碼:不設置自定義單元格標記:0。該標記是爲textLabel視圖保留的。如果您在Xcode Storyboard中設置了標籤,您必須將自定義視圖/標籤的標籤設置爲1或更高。

注意蘋果的例子:

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

    UILabel *label; 

    label = (UILabel *)[cell viewWithTag:1]; 
    label.text = [NSString stringWithFormat:@"%d", indexPath.row]; 

    label = (UILabel *)[cell viewWithTag:2]; 
    label.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row]; 

    return cell; 
} 
0

我會做如下:

UIImageView *chevronImgVw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron_accessory_vw_img.png"]]; 
chevronImgVw.frame = CGRectMake(cell.accessoryView.frame.origin.x, cell.accessoryView.frame.origin.y, 10, 20); 
cell.accessoryView = chevronImgVw; 

請嘗試在此之前你失望的一票!因爲目前它有2個upvotes和2個downvotes!

-1

我試過上面的代碼,它似乎不工作了,也許是由於這篇文章的年齡,所以我會拋棄我的行,給了我定製的配件視圖。

[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowBtn"]]]; 

希望有人認爲這有用。

0

有一個nice example from Apple顯示如何使用UIControl來完成這種accessoryView自定義。

在UIControl中重寫不是最簡單的方法,但給你很多靈活性,以風格漂亮的配件視圖。

enter image description here