Uitableview 设置自己的电池附件类型

Uitableview 设置自己的电池附件类型,uitableview,swift,xcode6,cell,accessorytype,Uitableview,Swift,Xcode6,Cell,Accessorytype,我想在UITableViewCell中设置我自己的cellAccessoryType(图像)。你知道我怎么做吗?我使用的是Swift、Xcode 6.2和iOS 8.2。谢谢你的帮助 试试这个: // first create UIImageView var imageView : UIImageView imageView = UIImageView(frame:CGRectMake(20, 20, 100, 320)) imageView.image = UIImage(named:"im

我想在UITableViewCell中设置我自己的cellAccessoryType(图像)。你知道我怎么做吗?我使用的是Swift、Xcode 6.2和iOS 8.2。谢谢你的帮助

试试这个:

// first create UIImageView
var imageView : UIImageView
imageView  = UIImageView(frame:CGRectMake(20, 20, 100, 320))
imageView.image = UIImage(named:"image.jpg")

// then set it as cellAccessoryType
cell.accessoryView = imageView

PS:我强烈建议您升级到XCode 6.3.2并使用iOS 8.3 SDK,我想您可能想点击附件视图,所以我用按钮提供了这个答案。 如果没有,请将shargath的答案与imageView一起使用

var saveButton = UIButton.buttonWithType(.Custom) as UIButton
        saveButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        saveButton.addTarget(self, action: "accessoryButtonTapped:", forControlEvents: .TouchUpInside)
        saveButton.setImage(UIImage(named: "check-circle"), forState: .Normal)
        cell.accessoryView = saveButton as UIView

func accessoryButtonTapped(sender:UIButton) {

}

Shmidt答案的Swift 5版本,以及使用sender.tag跟踪按钮单击的行:

override func tableView(_ tableView: UITableView, 
    cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", 
        for: indexPath)
    let checkButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    checkButton.addTarget(self, action:#selector(YOUR_CLASS_NAME.checkTapped(_:)), 
        for: .touchUpInside)
    checkButton.setImage(UIImage(named: "check"), for: .normal)
    checkButton.tag = indexPath.row
    cell.accessoryView = checkButton
    return cell
}


@objc func checkTapped(_ sender: UIButton) {
    print(sender.tag)
}

Swift 5版本,添加内容模式:

let imageView: UIImageView = UIImageView(frame:CGRect(x: 0, y: 0, width: 20, height: 20)) imageView.image = UIImage(named:Imge.Card.Link.download) imageView.contentMode = .scaleAspectFit cell.accessoryView = imageView 让imageView:UIImageView=UIImageView(帧:CGRect(x:0,y:0,宽度:20,高度:20)) imageView.image=UIImage(名称:Imge.Card.Link.download) imageView.contentMode=.ScaleSpectFit cell.accessoryView=imageView
Shmidt答案的Xamarin.iOS版本

// Create the accessory
var accessory = new UIButton(UIButtonType.Custom);
accessory.Frame = new CGRect(0f, 0f, 24f, 24f);
accessory.AddTarget(accessoryButtonTapped, UIControlEvent.TouchUpInside);
accessory.SetImage(UIImage.FromBundle("check-circle"), UIControlState.Normal);
// Set the accessory to the cell
cell.AccessoryView = accessory as UIView;

void accessoryButtonTapped(object sender, EventArgs args)
{

}

SWIFT 5解决方案

本示例介绍如何将带有SF符号的简单按钮添加到附件视图中

在cellForRowAt内:

let plusButton = UIButton(type: .system)
plusButton.setImage(UIImage(systemName: "plus"), for: .normal)
plusButton.contentMode = .scaleAspectFit
plusButton.sizeToFit()
cell.accessoryView = plusButton
我们可以使用UITableViewDelegate中的委托方法,而不是将目标添加到按钮

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
    //handle logic here
}

是否有可能发送“indexPath.section”和“indexPath.row”?@Markus是的,有几种不同的方法可以做到这一点,但简单的方法是将节和行组合成一个数字,如1018(第10节,第18行),并将其分配给标记。更多信息和其他方式:谢谢。非常有趣。乍一看,我想将2个数字合并为1(节+行),但我不知道如何检索值,因为节和行可以有1位数字、2位数字等。我不知道是否创建一个UIButton的子类,并使用一个额外的属性:IndexPath。