Select 并取消选择 UITableView 行
Select and Deselect UITableView Rows
我有包含多行的 TableView,当我取消选择任何行时,我正在选择它们并将它们添加到标签文本中,但我无法将其从标签文本中删除
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == categoryTable
{
let cell = self.categoryTable.dequeueReusableCellWithIdentifier("categoryCell") as! InquiryCategoryTableViewCell
let Category:CategoryDB = categoryData.objectAtIndex(indexPath.row) as! CategoryDB
print("Category = \(Category.category)")
cell.categoryName.text = "\(Category.category)"
cell.tintColor = color.UIColorFromRGB(0xCEEBFF)
categoryTable.allowsMultipleSelectionDuringEditing = true
categoryTable.setEditing(true, animated: false)
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
self.category_id = categoryList!.catg_id
print("Name = \(categoryList!.category)")
print("ID = \(self.category_id)")
categoryLabel.text! += "\(categoryList!.category), "
}
}
这是我得到的输出:
我首先选择了它附加到 Label.text 的 3 行,在我取消选择行后 Label.text保持不变
任何人都可以在这段代码中帮助我吗?
你只需要像下面那样实现UITableView
的委托方法
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
// update your label here
}
请使用数组来执行此操作。在顶部声明 NSMutableArray
var arrSelectedTexts:NSMutableArray = [String]()
现在
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(!arrSelectedTexts.containsObject(categoryList!.category))
arrSelectedTexts.addObject(categoryList!.category)
//Now update your label using the objects of the array
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if(arrSelectedTexts.containsObject(categoryList!.category))
arrSelectedTexts.removeObject(categoryList!.category)
//Now update your label using the objects of the array
}
您可以在 CategoryDB 中添加标志 - hasSelected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
categoryList!.hasSelected = true
refreshLabel()
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
categoryList!.hasSelected = false
refreshLabel()
}
}
func refreshLabel(){
var label = ""
for object in categoryData as! CategoryDB {
if(object.hasSelected){
label += "\(categoryList!.category), "
}
}
categoryLabel.text! = label
}
首先从 cellForRowAtIndexPath
中删除 categoryTable.allowsMultipleSelectionDuringEditing = true
并将其添加到 viewDidload
,因为 cellForRowAtIndexPath
很多时候您的单元会重复使用,因此无需设置那么多次!
现在您还应该实现 didDeselectRowAtIndexPath
委托方法。
在该方法中,您将获得 indexPath.row
行 deSelect
或 unselect
,从 indexPath.row
您可以从数组中获取对象您需要从标签中显示的字符串中删除。
因此,在 didDeselectRowAtIndexPath
中,首先转换 your label text to mutable array
然后从该数组中删除对象,然后再次将该数组转换为字符串并显示在标签上!
试试这个,它非常适合我,
考虑这个样本变量
let animals : [String] = ["Dog","Cat","Lion","Tiger","Cow","Buffalo","Horse"]
var sampleArray = [String]()
var samleLabel = UILabel()
在您的 didSelectRowAtIndexPath 中添加此功能
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.textLabel!.text = animals [indexPath.row]
if sampleArray.contains((cell.textLabel?.text)!)
{
sampleArray.removeAtIndex(indexPath.row)
print(sampleArray)
samleLabel.text = test().componentsJoinedByString(",")
}
else
{
sampleArray.append((cell.textLabel?.text)!)
print(sampleArray)
samleLabel.text = test().componentsJoinedByString(",")
}
}
func test()-> NSArray
{
print(sampleArray)
return sampleArray;
}
我有包含多行的 TableView,当我取消选择任何行时,我正在选择它们并将它们添加到标签文本中,但我无法将其从标签文本中删除
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == categoryTable
{
let cell = self.categoryTable.dequeueReusableCellWithIdentifier("categoryCell") as! InquiryCategoryTableViewCell
let Category:CategoryDB = categoryData.objectAtIndex(indexPath.row) as! CategoryDB
print("Category = \(Category.category)")
cell.categoryName.text = "\(Category.category)"
cell.tintColor = color.UIColorFromRGB(0xCEEBFF)
categoryTable.allowsMultipleSelectionDuringEditing = true
categoryTable.setEditing(true, animated: false)
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
self.category_id = categoryList!.catg_id
print("Name = \(categoryList!.category)")
print("ID = \(self.category_id)")
categoryLabel.text! += "\(categoryList!.category), "
}
}
这是我得到的输出:
我首先选择了它附加到 Label.text 的 3 行,在我取消选择行后 Label.text保持不变
任何人都可以在这段代码中帮助我吗?
你只需要像下面那样实现UITableView
的委托方法
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
// update your label here
}
请使用数组来执行此操作。在顶部声明 NSMutableArray
var arrSelectedTexts:NSMutableArray = [String]()
现在
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(!arrSelectedTexts.containsObject(categoryList!.category))
arrSelectedTexts.addObject(categoryList!.category)
//Now update your label using the objects of the array
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if(arrSelectedTexts.containsObject(categoryList!.category))
arrSelectedTexts.removeObject(categoryList!.category)
//Now update your label using the objects of the array
}
您可以在 CategoryDB 中添加标志 - hasSelected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
categoryList!.hasSelected = true
refreshLabel()
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
categoryList!.hasSelected = false
refreshLabel()
}
}
func refreshLabel(){
var label = ""
for object in categoryData as! CategoryDB {
if(object.hasSelected){
label += "\(categoryList!.category), "
}
}
categoryLabel.text! = label
}
首先从 cellForRowAtIndexPath
中删除 categoryTable.allowsMultipleSelectionDuringEditing = true
并将其添加到 viewDidload
,因为 cellForRowAtIndexPath
很多时候您的单元会重复使用,因此无需设置那么多次!
现在您还应该实现 didDeselectRowAtIndexPath
委托方法。
在该方法中,您将获得 indexPath.row
行 deSelect
或 unselect
,从 indexPath.row
您可以从数组中获取对象您需要从标签中显示的字符串中删除。
因此,在 didDeselectRowAtIndexPath
中,首先转换 your label text to mutable array
然后从该数组中删除对象,然后再次将该数组转换为字符串并显示在标签上!
试试这个,它非常适合我,
考虑这个样本变量
let animals : [String] = ["Dog","Cat","Lion","Tiger","Cow","Buffalo","Horse"]
var sampleArray = [String]()
var samleLabel = UILabel()
在您的 didSelectRowAtIndexPath 中添加此功能
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.textLabel!.text = animals [indexPath.row]
if sampleArray.contains((cell.textLabel?.text)!)
{
sampleArray.removeAtIndex(indexPath.row)
print(sampleArray)
samleLabel.text = test().componentsJoinedByString(",")
}
else
{
sampleArray.append((cell.textLabel?.text)!)
print(sampleArray)
samleLabel.text = test().componentsJoinedByString(",")
}
}
func test()-> NSArray
{
print(sampleArray)
return sampleArray;
}