我想在我的UITableView中使用標準披露附件圖像的自定義版本。我怎樣才能做到這一點?我希望子類UITableViewCell對於這個基本的東西不是必需的。如何在UITableViewCell中定製附件披露圖像?
48
A
回答
113
您需要創建自定義視圖並將其分配給UITableViewCell
對象的accessoryView
屬性。喜歡的東西:
myCell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"Something" ]];
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中重寫不是最簡單的方法,但給你很多靈活性,以風格漂亮的配件視圖。
相關問題
- 1. 隱藏/顯示UITableViewCell的附件披露指標
- 2. 如何將披露按鈕添加到自定義UITableViewCell
- 3. 披露指標和詳細披露
- 4. 漸進式披露控制
- 5. 如何將披露按鈕設置爲UItableViewCell
- 6. UITableViewCell披露指示器在滾動中消失
- 7. 具有自定義披露的UITableViewCell會導致錯誤對齊?
- 8. 在採取行動後向UITableViewCell添加細節披露?
- 9. iOS在UITableViewCell上與iOS9 iPad發生錯誤披露背景
- 10. 披露三角沒有出現在我的UITableViewCell上
- 11. 私有IP披露
- 12. JavaFX中的披露窗格
- 13. 如何使用披露附件按鈕創建像單元格一樣的視圖?
- 14. 定製UITableViewCell附件未正確定位
- 15. 細節披露按鈕和披露指標有什麼區別?
- 16. 如何強制UITableViewCell保留其附件
- 17. 如何製作基於模塊化NSViewController的披露視圖,其中所有披露的視圖都包含在他們自己的NIB中?
- 18. 的UITableViewCell +標準披露指標+次要的UIButton + HIG違反
- 19. 旋轉UITableViewCell細節披露指標不工作
- 20. 在flex表中添加披露面板
- 21. 在ADF樹中披露節點
- 22. 表視圖細節披露iOS
- 23. 詳細披露圖標的位置
- 24. UITableViewCell的定製附件按鈕
- 25. 自定義UIView信息披露指標
- 26. 哪裏是詳細披露按鈕圖像存儲?
- 27. 安全風險披露php文件名
- 28. GWT披露面板bug
- 29. 添加披露按鈕MKPointAnnotation
- 30. CGI信息披露問題
如果我這樣做,我注意到的UITableViewDelegate方法 - [的tableView:accessoryButtonTappedForRowWithIndexPath:]不再被調用。那是預期的行爲?有沒有辦法保持這種方法的工作? – 2010-01-05 01:54:47
還有一些更多的解釋在http://stackoverflow.com/questions/869421/using-a-custom-image-for-a-uitableviewcells-accessoryview-and-having-it-respond – jarnoan 2010-10-06 13:21:45
是否有可能使用UIApperance UITableViewCell上的代理? – Claus 2013-07-03 10:37:11