ios - automatically define current index -
i have controller buttons , tableviewcontroller
10 arrays. each button has index, pass tableviewcontroller
.
in tableviewcontroller
code looks this:
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: string(format: "cell", indexpath.row), for: indexpath) if buttonindex == 0 { label?.text = array0[indexpath.row] } else if buttonindex == 1 { label?.text = array1[indexpath.row] } else if buttonindex == 2 { label?.text = array2[indexpath.row] } else if buttonindex == 3 { label?.text = array3[indexpath.row] } else if buttonindex == 4 { label?.text = array4[indexpath.row] } else if buttonindex == 5 { label?.text = array5[indexpath.row] } else if buttonindex == 6 { label?.text = array6[indexpath.row] } else if buttonindex == 7 { label?.text = array6[indexpath.row] } else if buttonindex == 8 { label?.text = array6[indexpath.row] } }
i want automatically define current index this:
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: string(format: "cell", indexpath.row), for: indexpath) //label?.text = array(currentindex[indexpath.row] }
how it?
you cannot generate variable names dynamically since have known in runtime.
one feasible solution put arrays in array , access individual arrays through indexes in array of arrays.
let allarrays: [[any]] = [array0,array1,...] override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: string(format: "cell", indexpath.row), for: indexpath) label?.text = allarrays[buttonindex][indexpath.row] }
Comments
Post a Comment