动态设置可重用tableViewCell的标识符

时间:2018-02-12 11:18:20

标签: ios objective-c uitableview ui-testing

tableViewCell包含两个标签,每次迭代都会更新标签的内容。对于UI测试,我需要设置标签的标识符以检查标签的内容是否与预期的相同。 如何动态设置可重复使用单元格内标签的标识符?

@interface MyDetailsTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIDLabel *myDetailsTitlelabel;
@property (weak, nonatomic) IBOutlet UIDLabel *myDetailsContentLabel;

@end

这些是我的tableViewCell中的两个标签。

1 个答案:

答案 0 :(得分:0)

我假设您的tableView已设置并正常工作。

cellForRowAt功能中,您可以为每一行动态设置ID。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "<your_reuse_identifier>", for: indexPath) as! MyDetailsTableViewCell
    cell.myDetailsTitlelabel.text = "id" // set your label text here    
    cell.myDetailsTitlelabel.accessibilityIdentifier = "id" // set your dynamic id here
    cell.myDetailsContentLabel.text = "id" // set your label text here    
    cell.myDetailsContentLabel.accessibilityIdentifier = "id" // set your dynamic id here
    return cell
}