[英]How to add a UIbutton in a TableViewCell?
我嘗試將UIButton
添加到TableViewCell
,但是我意識到我無法添加插座,因為我的單元格正在重復。
如何在.m
文件中以IBOutlet
訪問UIButton
?
嘗試這個:
UITableViewCell *cell = [tableview deque....];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
UIButton *btn = [[UIButton alloc]initWithFrame:frame];
[cell.contentView addsubview:btn];
}
在CellForRowAtIndexPath中添加它
只需在@implementation ScheduleDateCell上方的TableViewCell.m文件中聲明一個類擴展,如下所示
@interface TableViewCell ()
{
}
@end
然后在xib中可以添加一個按鈕,然后在助手編輯器模式下單擊鼠標右鍵將其拖到.m文件。
您需要為UITableViewCell創建一個單獨的類,然后可以創建button的出口,然后可以從自定義單元格類對象訪問button。 例如,創建UITableViewCell自定義類的對象:
KBTableViewCell *cell; //here KBTableViewCell is custom class for UITableViewCell
然后在您的cellForRowAtIndexPath中:
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.btnMore.tag = indexPath.row; //here btnMore is my button, here do whatever you want to
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.