Swift只允许选择一个单元格

时间:2015-08-29 10:05:37

标签: ios xcode swift uitableview

我有一个工作的UITableview,目前允许选择多个Cell。我希望只选择一个,如果选择了前一个,则新选择应取消选中前一个。介意弯曲!这是我的代码:

import tkinter

class Application(tkinter.Tk):

  TITLE = 'Simultaneous Widget Blink Test'
  BLINK_INT = 75

  def __init__(self, rows=4, cols=10):
    super().__init__()
    self.rows, self.cols = rows, cols
    self.frames = [[] for _ in range(rows)]
    self.labels = [[] for _ in range(rows)]
    self.withdraw()  # assembling in background...
    self.resizable(width=False, height=False)
    self.title(self.TITLE)
    self.build()
    for ri in range(self.rows):
      self.curr_ri = ri
      self.set_row_style()
    self.curr_ri, self.curr_n = 0, 0
    self.deiconify()  # showing it
    self.blink_again()

  def build(self):
    for ri in range(self.rows):
      for ci in range(self.cols):
        f = tkinter.Frame(self, width=40, height=40)
        self.frames[ri].append(f)
        f.grid(row=ri, column=ci, sticky='news')
        l = tkinter.Label(f, text='{}{}'.format(ri, ci))
        self.labels[ri].append(l)
        l.place(relx=0.5, rely=0.5, anchor='c')

  def blink_again(self):
    if self.curr_n == 10:
      self.curr_n = 0
      self.curr_ri += 1
    if self.curr_ri == self.rows:
      self.curr_ri = 0
    self.curr_n += 1
    self.highlight_row()

  def highlight_row(self):
    self.set_row_style(bg='orange')
    self.after(self.BLINK_INT, self.restore_row)

  def restore_row(self):
    self.set_row_style()
    self.after(self.BLINK_INT, self.blink_again)

  def set_row_style(self, bg='white'):
    for w in self.frames[self.curr_ri]:
      w.configure(bg=bg)
    for w in self.labels[self.curr_ri]:
      w.configure(bg=bg)

if __name__ == '__main__':
  app = Application()
  app.mainloop()

2 个答案:

答案 0 :(得分:4)

管理选择状态的一种非常有效的方法是向数据模型添加selected属性,在此示例中称为Model

class Model {

  var selected = false

  ...
}

UITableViewController类中,我们假设有一个数组data,它将Model个项目作为表视图的数据源。

var data = [Model]()

cellForRowAtIndexPath中设置图片和文字取决于selected属性:

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as! UITableViewCell
    let currentItem = data[indexPath.row]
    if currentItem.selected {
      cell.imageView!.image = UIImage(named:"check")!
      cell.textLabel!.font = UIFont(name:"OpenSans-Bold", size:15)
    } else {
      cell.imageView!.image = nil
      cell.textLabel!.font = UIFont(name:"OpenSans-Regular", size:15)
    }

    return cell
  }

didSelectRowAtIndexPath中将当前所选单元格的属性selected设置为false,将刚刚选定单元格的属性selected设置为true并重新加载表格,更改将应用​​于cellForRowAtIndexPath

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    data.filter {$0.selected == true}.map {$0.selected = false}
    let currentItem = data[indexPath.row]
    currentItem.selected = true
    tableView.reloadData()
  }

答案 1 :(得分:0)

您可以创建实例var以保留选择索引

然后在你的cellForINdexPath方法中检查当前单元格是否等于它:

这里讨论过: How to uncheck all rows using UITableViewCellAccessoryCheckmark