2015-02-06 13 views
7

Good Morningxem bảng của tôi tái sử dụng các ô được chọn khi cuộn - trong SWIFT

vấn đề của tôi là xem bảng của tôi tái sử dụng các ô đã chọn khi tôi di chuyển xuống và lên một lần nữa .. tôi có nghĩa là khi tôi chọn một tế bào từ sau đó cuộn xuống, một số ô mà tôi không chọn được hiển thị được chọn, một số ô được chọn không hiển thị được chọn khi tôi cuộn lên lần nữa và khi điều đó xảy ra, tôi lại là táo để chọn nhiều hơn một ô mà không phải cho phép .. tôi muốn đề cập đến rằng tôi đã cố gắng để lưu đường dẫn chỉ mục cũ từ 'didSelectRowAtIndexPath'and tôi đã kiểm tra nó như thế này trong' CellForRowAtIndexPath'but nó không hoạt động:

if (old == indexpath) 
{ 
     cell?.backgroundColor = UIColor.redColor() 
     cell?.textLabel?.backgroundColor = UIColor.whiteColor() //to change only the thumbnail color and keep the cell color 
} 

else { 
     cell?.backgroundColor = UIColor.whiteColor() 
} 

Bây giờ ở đây là mã của tôi

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = myTable.dequeueReusableCellWithIdentifier("WorkCell") as UITableViewCell 
    cell.tag = (indexPath.row) + 1 
    cell.textLabel?.text = Berufs[indexPath.row] 
    var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40)) 
    cell.selectionStyle = UITableViewCellSelectionStyle.None 
    img.image = UIImage(named: "transparent.png") 
    cell.indentationLevel = 1 
    cell.indentationWidth = 45 
    cell.addSubview(img) 
    return cell 
} 

var old = 1000 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    let indexPath = tableView.indexPathForSelectedRow() 
    let cell = tableView.cellForRowAtIndexPath(indexPath!) 
    var tmpCell = view.viewWithTag(old) 
    var rowNum = (cell?.tag)! - 1 

    if (cell?.backgroundColor == UIColor.redColor()) 
    { 
     cell?.backgroundColor = UIColor.whiteColor() 
    } 
    else if (cell?.backgroundColor != UIColor.redColor()) 
    { 
     tmpCell?.backgroundColor = UIColor.whiteColor() 
     cell?.backgroundColor = UIColor.redColor() 
     cell?.textLabel?.backgroundColor = UIColor.whiteColor() 

     println("du interssiert dich für \(Berufs[rowNum])") 
    } 

    old = rowNum + 1 

} 

Trả lời

14

Bạn hiện lưu trữ nhà nước lựa chọn trong backgroundColor . Đó không phải là một ý tưởng tốt bởi vì vì lý do hiệu suất, các ô sẽ được sử dụng lại bởi tableView.

Bạn nên lưu các indexPath đã chọn trong mô hình dữ liệu. Nếu bạn muốn cho phép nhiều lựa chọn, bạn có thể lưu trữ các indexPath đã chọn trong một NSMutableSet.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
    cell.selectionStyle = .None 
    configure(cell, forRowAtIndexPath: indexPath) 
    return cell 
} 

var selectedIndexPaths = NSMutableSet() 

func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.textLabel!.text = "Row \(indexPath.row)" 
    if selectedIndexPaths.containsObject(indexPath) { 
     // selected 
     cell.backgroundColor = UIColor.redColor() 
    } 
    else { 
     // not selected 
     cell.backgroundColor = UIColor.whiteColor() 
    } 
} 


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if selectedIndexPaths.containsObject(indexPath) { 
     // deselect 
     selectedIndexPaths.removeObject(indexPath) 
    } 
    else { 
     // select 
     selectedIndexPaths.addObject(indexPath) 
    } 
    let cell = tableView.cellForRowAtIndexPath(indexPath)! 
    configure(cell, forRowAtIndexPath: indexPath) 
} 

Nếu bạn chỉ cần lựa chọn duy nhất, bạn nên lưu tệp chỉ mục đã chọn trong NSIndexPath? biến mẫu

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
    cell.selectionStyle = .None 
    configure(cell, forRowAtIndexPath: indexPath) 
    return cell 
} 

var selectedIndexPath: NSIndexPath? 

func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.textLabel!.text = "Row \(indexPath.row)" 
    if selectedIndexPath == indexPath { 
     // selected 
     cell.backgroundColor = UIColor.redColor() 
    } 
    else { 
     // not selected 
     cell.backgroundColor = UIColor.whiteColor() 
    } 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if selectedIndexPath == indexPath { 
     // selected same cell -> deselect all 
     selectedIndexPath = nil 
    } 
    else { 
     // select different cell 
     let oldSelectedIndexPath = selectedIndexPath 
     selectedIndexPath = indexPath 

     // refresh old cell to clear old selection indicators 
     var previousSelectedCell: UITableViewCell? 
     if let previousSelectedIndexPath = oldSelectedIndexPath { 
      if let previousSelectedCell = tableView.cellForRowAtIndexPath(previousSelectedIndexPath) { 
       configure(previousSelectedCell, forRowAtIndexPath: previousSelectedIndexPath) 
      } 
     } 
    } 
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! 
    configure(selectedCell, forRowAtIndexPath: indexPath) 
} 
+0

hoạt động như một nét duyên dáng * __ * thankssssssss –

+0

câu trả lời này thật tuyệt vời và hoạt động như một sự quyến rũ, nhưng tại sao bạn lại ghi đè lên trước một số chức năng? miễn là bộ điều khiển thực hiện UITableViewDelegate, bạn chỉ nên có chức năng nhưng không có "ghi đè" phải không? –

+0

cảm ơn U đã tiết kiệm thời gian của tôi quá :) – user1553381

-1

tôi đã cùng một vấn đề trong một thời gian trước, những gì tôi đã làm là, tôi đã thêm một biến mới (Bool) có tên isset đến CustomTableViewCell, và khi tôi tạo ra các tế bào cho lần đầu tiên, tôi đã thay đổi nó là true, vì vậy sau khi tôi muốn tái sử dụng một tế bào đã được thiết lập, tôi chỉ trả lại arleady thiết lập một, để không đặt là một lần nữa:

let cell = tableView.dequeueReusableCellWithIdentifier("Cellidentifier", forIndexPath: indexPath) as CustomTableViewCell 

if cell.isset 
{ 
     return cell 
} 

//set your cell here 
cell.isset = true 
return cell 
+0

tôi không tìm thấy hàm này 'isset', bạn đã tự tạo nó hay là nó được xây dựng? –

+0

Bạn đã thử chạy mã hay bạn gặp lỗi? Đôi khi XCode không hiển thị tất cả các phương thức. Và bạn đã thực hiện mã này trong cellForRowAtIndexPath phải không? –

+0

nó cho thấy một lỗi .. có .. tôi đã làm nó có –

Các vấn đề liên quan