2015-11-11 3 views
5

、私は画像テキスト両方でボタンを実装しようとしています。Swift:UITableViewCellでUIViewをクリック可能にする方法<code>UITableViewCell</code>インサイド

標準のようですUIButtonこれを達成できません。だからUIViewを作成し、これにはUIImageViewUILabelが含まれています。あなたは右側に、ここでの実装を見ることができます

、「旅に従う」ボタン(「+」は、「旅に従ってください」UIImageViewで、UILabelである)

enter image description here

IそのようなUIView(つまりボタン)をクリック可能にしようとしていますが、私は方法を見つけることができません。

これは私の実装ですが、それは動作しません:

class StationsIntroHeader: UITableViewCell { 

    @IBOutlet weak var bigButton: UIView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     let tap = UITapGestureRecognizer(target: self, action: Selector("followTrip:")) 
     bigButton.addGestureRecognizer(tap) 
    } 

    func followTrip(sender:UITapGestureRecognizer) { 
     print("tap working") 
    } 
} 

私はを有効ユーザー操作がUIImageViewUILabel

+2

あなただけで、オレンジ色の画像を行い、プラスにも、あなたのボタンの背景画像として署名し、右のフォロートリップラベルを揃える、またはすることができますビューと同じサイズのカスタムボタンを追加し、透明な背景とラベルなしでビューに追加することができます。 –

+1

タッチは「UITableView」によって「吸収」されています。代わりに 'UITableView'にジェスチャ認識子を追加し、座標を使用してどのセルとどのボタンがクリックされたかを把握してください。 –

+0

あなたのような実装は完全に機能しています!すべてを正しく設定していますか? –

答えて

4

次のようなサンプルのセットアップが完全に動作します:

class TableViewController: UITableViewController { 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    return tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) 
    } 
} 

class CustomCell: UITableViewCell { 
    @IBOutlet weak var bigButton: UIView! 

    override func awakeFromNib() { 
    super.awakeFromNib() 
    let tap = UITapGestureRecognizer(target: self, action: Selector("bigButtonTapped:")) 
    bigButton.addGestureRecognizer(tap) 
    } 

    func bigButtonTapped(sender: UITapGestureRecognizer) { 
    print("bigButtonTapped") 
    } 
} 

私はのデフォルト値のいずれも変更しませんでしたビューまたは画像ビューまたはラベルの場合は。私の実装をあなたのものと比較して、あなたが何かを忘れたかどうかを見てみましょう。コンセントを接続しますか?

サンプルプロジェクト:プロジェクトのhttps://www.dropbox.com/sh/hpetivhc3gfrapf/AAAf6aJ0zhvRINPFJHD-iMvya?dl=0

編集

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerCell = tableView.dequeueReusableCellWithIdentifier("StationsIntroHeader") as! StationsIntroHeader 
    headerCell.update() 
    return headerCell 
    // return headerCell.update().contentView 
} 
+0

こんにちはアンドレ!私はちょうどあなたのプロジェクトを実行し、それは実際に動作します!私は現在、コントロールのInterface Builderの設定を比較して差異を見つけようとしています –

+0

何か問題が見つからない場合は、ドロップボックスからプロジェクトを共有してください。 –

+0

修正が見つかりません!私は簡易版をアップロードしました:https://www.dropbox.com/sh/rgfvrn1jpqcfloj/AADEar4aryx08rwb6je5s_mqa?dl=0 –

1

の両方でUIViewとOFFにONであることを確認してきましたあなたは単にUIButtonをサブクラス化し、その中にカスタム図面を実装することができます。 UIButtonは、テーブルビューの接触を妨げずにタスクを実行する最も簡単な方法です。テーブルビューでタップジェスチャーを実装すると、セルに問題が発生します。

カスタム画像(1つの画像として+記号とテキストの両方)を取得し、それを背景画像として使用できます。

コードで描画することもできます。

たとえば、あなたはこの試みることができます:

func drawCanvas1(frame frame: CGRect = CGRectMake(3, 8, 209, 109)) { 
    //// General Declarations 
    let context = UIGraphicsGetCurrentContext() 

    //// Color Declarations 
    let color = UIColor(red: 0.967, green: 0.423, blue: 0.211, alpha: 1.000) 

    //// Image Declarations 
    let screenShot20151111At32900PM = UIImage(named: "screenShot20151111At32900PM.png")! 

    //// Rectangle Drawing 
    let rectanglePath = UIBezierPath(roundedRect: CGRectMake(frame.minX + 39, frame.minY + 23, 113, 46), cornerRadius: 8) 
    color.setFill() 
    rectanglePath.fill() 


    //// Rectangle 2 Drawing 
    let rectangle2Rect = CGRectMake(frame.minX + 51, frame.minY + 27, 33, 34) 
    let rectangle2Path = UIBezierPath(rect: rectangle2Rect) 
    CGContextSaveGState(context) 
    rectangle2Path.addClip() 
    screenShot20151111At32900PM.drawInRect(CGRectMake(floor(rectangle2Rect.minX - 16 + 0.5), floor(rectangle2Rect.minY - 15 + 0.5), screenShot20151111At32900PM.size.width, screenShot20151111At32900PM.size.height)) 
    CGContextRestoreGState(context) 


    //// Text Drawing 
    let textRect = CGRectMake(frame.minX + 97, frame.minY + 23, 73, 46) 
    let textTextContent = NSString(string: "\nfollow\ntrip\n") 
    let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle 
    textStyle.alignment = .Left 

    let textFontAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.labelFontSize()), NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textStyle] 

    let textTextHeight: CGFloat = textTextContent.boundingRectWithSize(CGSizeMake(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size.height 
    CGContextSaveGState(context) 
    CGContextClipToRect(context, textRect); 
    textTextContent.drawInRect(CGRectMake(textRect.minX, textRect.minY + (textRect.height - textTextHeight)/2, textRect.width, textTextHeight), withAttributes: textFontAttributes) 
    CGContextRestoreGState(context) 
} 

をし、その結果のようなものになります。私のため

enter image description here

関連する問題