选择并取消选择UITableView Rows

时间:2016-11-17 07:18:14

标签: ios uitableview swift2 multipleselection uitableviewrowaction

我有多行的TableView,当我取消选择我无法从标签文本中删除它的任何行时,我选择它们并将它们添加到Label文本

这是我的代码:

// In your controler before send mail : 
Yii::$app->mailer->view->params['title'] = $title;

// In your layout
echo $this->params['title'];

这是我得到的输出:

我首先选择了附加到 Label.text 的3行,然后取消选择行 Label.text 保持不变

任何可以帮助我使用此代码的人?

5 个答案:

答案 0 :(得分:2)

尝试这个,它完全适合我,

考虑这个样本变量

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;
}

答案 1 :(得分:1)

请使用数组执行此操作。在顶部声明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
    }

答案 2 :(得分:1)

你可以在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

}

答案 3 :(得分:1)

首先从categoryTable.allowsMultipleSelectionDuringEditing = true删除cellForRowAtIndexPath并将其添加到viewDidload,因为cellForRowAtIndexPath很多时候您的单元格重复使用,因此无需多次设置它!

现在你也应该实现didDeselectRowAtIndexPath委托方法。

在该方法中,您将获得indexPath.rowdeSelect行的unselect,从indexPath.row您可以从阵列中获取您需要的对象从标签中显示的字符串中删除。

所以,在didDeselectRowAtIndexPath首先转换your label text to mutable array然后从该数组中删除对象,然后再将该数组转换为字符串并在标签上显示!

答案 4 :(得分:0)

您只需要实现- (void)keepAlive{ int i; if (!pj_thread_is_registered()) { pj_thread_register("ipjsua", a_thread_desc, &a_thread); } /* Since iOS requires that the minimum keep alive interval is 600s, * application needs to make sure that the account's registration * timeout is long enough. */ for (i = 0; i < (int)pjsua_acc_get_count(); ++i) { if (pjsua_acc_is_valid(i)) { pjsua_acc_set_registration(i, PJ_TRUE); } } pj_thread_sleep(15000);} 的委托方法,如下所示

UITableView