我想像以下那样循环:
class cellVC: UICollectionViewCell {
@IBOutlet weak var sampleLabel: UILabel!
@IBOutlet weak var buttonClicked: UIButton!
var buttonTapAction: ((cellVC) -> ())?
@IBAction func buttonTest(_ sender: Any) {
buttonTapAction?(self)
}
}
//////////
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell : cellVC = collectionView.dequeueReusableCell(withReuseIdentifier: "cellVC", for: indexPath) as! cellVC
cell.sampleLabel. = "sample text"
cell.buttonClicked.tag = indexPath.row
cell.buttonTapAction = { cell in
//Tap handling logic
}
cell.buttonClicked.backgroundColor = UIColor.cyan
return cell
}
我尝试使用for j in range(1,4) and for k in ['one', 'two', 'three']:
print(str(j) + ' is written ' + k)
,但它没有用。有人如何得到这种效果?
如果两个列表的长度不同,会发生什么?我怎么还能遍历它们呢?
答案 0 :(得分:7)
您可以使用zip
for j, k in zip(range(1,4), ['one', 'two', 'three']):
print('{} is written {}'.format(j, k))
1 is written one
2 is written two
3 is written three
如果一个比另一个长,您可以考虑使用itertools.zip_longest
答案 1 :(得分:7)
检查出来:
for j,k in enumerate(['one', 'two', 'three'], 1):
print("{} is written {}".format(j, k))
答案 2 :(得分:4)
你应该zip
全部!
for j, k in zip(range(1,4), ("one", "two", "three")):
# use j and k