Ios 如何在TableViewCell中添加UIbutton?

Ios 如何在TableViewCell中添加UIbutton?,ios,objective-c,Ios,Objective C,我尝试向TableViewCell添加UIButton,但我意识到我无法添加插座,因为我的单元格正在重复 如何将UIButton作为.m文件中的IBOutlet访问?试试以下方法: UITableViewCell *cell = [tableview deque....]; if(cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:

我尝试向TableViewCell添加UIButton,但我意识到我无法添加插座,因为我的单元格正在重复

如何将UIButton作为.m文件中的IBOutlet访问?

试试以下方法:

UITableViewCell *cell = [tableview deque....];

if(cell == nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];

    UIButton *btn = [[UIButton alloc]initWithFrame:frame];
    [cell.contentView addsubview:btn];
}

将其添加到CellForRowatineXpath中

只需在上面的TableViewCell.m文件中声明一个类扩展名即可 @实现ScheduleDateCell如下所示

@interface TableViewCell ()
{

}
@end

然后在xib中,您可以添加一个按钮,然后在助手编辑器模式下右键单击并将按钮拖动到.m文件中

您需要为UITableViewCell创建一个单独的类,然后可以创建按钮的出口,然后您可以从自定义单元格类对象访问按钮。例如,创建UITableViewCell自定义类的对象:

KBTableViewCell *cell;   //here KBTableViewCell is custom class for UITableViewCell
然后在您的CellForRowatineXpath中:

cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.btnMore.tag = indexPath.row; //here btnMore is my button, here do whatever you want to

您可以使用标记代替outlet,也可以使用单独的类文件作为单元格的输出tableview@farhan你是否对单元格进行子类划分?你面临的问题是什么..我们为按钮设置了标签以再次获取它们。我想你应该把按钮的标签设置为常量。btn.tag=我的按钮标签。