3

テーブル ビューのセルを選択するときにハイライトを削除しようとしていますが、表示されるチェックマークを保持したいです。

cellForRowAtIndexPath でこれを試すと、次のようになります。

    cell.backgroundView = UIView()
    cell.backgroundView?.backgroundColor = UIColor.clearColor()

行全体ではなく、チェックマークの下のハイライトのみを削除します(添付の画像を参照)。

ここに画像の説明を入力

cellForRowAtIndexPath でこれを試すと、次のようになります。

    cell.selectionStyle = UITableViewCellSelectionStyle.None

チェックマークが表示されなくなりました。

更新:これを試してみましたが、チェックマークを付けなくなった cell.selectionStyle と同じことを行います

func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

これを行う良い方法は何ですか?チェックボックスのように機能させたいのですが、青いハイライトを発生させたくありません。TableView は動的に生成されます。

    checkBoxView = UITableView()
    checkBoxView.frame = CGRect(x: qView.bounds.midX, y: qView.bounds.midY, width: qView.bounds.width - 100, height: qView.bounds.height/1.5)
    checkBoxView.center = CGPointMake(qView.bounds.midX, qView.bounds.midY)
    checkBoxView.delegate = self
    checkBoxView.dataSource = self
    checkBoxView.tag = 100
    checkBoxView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    checkBoxView.setEditing(true, animated: true)
    self.qView.addSubview(checkBoxView)

テーブル ビュー機能:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.checkBoxContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = checkBoxView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    cell.textLabel?.text = self.checkBoxContent[indexPath.row]
    cell.backgroundColor = UIColor.clearColor()
    cell.tintColor = UIColor.greenColor()
    return cell
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle(rawValue: 3)!
}
4

4 に答える 4

5

チェックマークを付けたままにするために、このshouldHighlightRowAtIndexPathメソッドに false を設定することはできません。そうすると、左側にチェックマークがまったく表示されなくなります。

私がやったことは、セルの「selectedBackgroundView」を変更して、左側のチェックマークを保持し、背景色を設定する機会を与えることです。ここにいくつかのコードを同封し、それが役立つことを願っています.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! RecordTableViewCell
cell.selectedBackgroundView = UIView(frame: cell.frame)
cell.selectedBackgroundView?.backgroundColor = UIColor.orangeColor()

return cell
}
于 2015-12-07T07:33:17.530 に答える
0

スウィフト 1.2 の使用:

tableView.deselectRowAtIndexPath(indexPath、アニメーション: true)

didSelectRowAtIndexPath デリゲート メソッド内。

于 2015-08-12T09:48:03.607 に答える
-1

これを使用するUITableViewでこれを行う公式の方法があります:

   optional func tableView(_ tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool

このメソッドに対して YES を返すと、テーブルビューはクリック時にセルを強調表示します。

また、それを使用したくない場合は、セルの backgroundColor だけでなく、contentView.backgroundColor を変更する必要があることに注意してください。しかし、ハイライトルートは下るのに最適なルートです。

ここのドキュメント: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:shouldHighlightRowAtIndexPath :

于 2015-07-13T18:08:55.527 に答える