英文:
How to replace array values with new value in same index in swift
问题
I am showing dropdown in tableview cell here if i select one value from from dropdown i need to replace that selected value in existing array index
for eg: i got array like this
before array ----- ["12-R", "14-R", "13-R"]
here is my dropdown button code: initially here arrSelectedRowsBuyNew
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EventslistPopupCell", for: indexPath) as! EventslistPopupCell
cell.actionBlock = { tapAct in
self.showDropDown(with: strArray, from: tapAct) { (item, index) in
cell.dropdownTF.text = item
if index == 0{
self.arrSelectedRowsBuyNew.append(self.popupData[indexPath.row].selectedID.description + "-R")
}
if index == 1{
self.arrSelectedRowsBuyNew.append(self.popupData[indexPath.row].selectedID.description + "-V")
}
print("after array \(self.arrSelectedRowsBuyNew)")
}
}
return cell
}
for above code o/p: here i need to replace "12-R" with "12-V" and "14-R" with "14-V" here how to check the index and replace the new value
NOTE: here first two digits will be same for old and new array. so can we check and replace with that. first two digits coming from this self.popupData[indexPath.row].selectedID
please do help got stuck here from long time
after array ["12-R", "14-R", "13-R", "14-V", "12-V"]
EDIT:
before replace array before array ----- ["12-R", "14-R"]
initially all values at index 0
now if i select value from first cell dropdown then its replacing like this
error:
["12-R", "12-V"]
but i need like ["12-V", "14-R"]
英文:
I am showing dropdown in tableview cell here if i select one value from from dropdown i need to replace that selected value in existing array index
for eg: i got array like this
before array ----- ["12-R", "14-R", "13-R"]
here is my dropdown button code: initially here arrSelectedRowsBuyNew
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EventslistPopupCell", for: indexPath) as! EventslistPopupCell
cell.actionBlock = { tapAct in
self.showDropDown(with: strArray, from: tapAct) { (item, index) in
cell.dropdownTF.text = item
if index == 0{
self.arrSelectedRowsBuyNew.append(self.popupData[indexPath.row].selectedID.description + "-" + "R")
}
if index == 1{
self.arrSelectedRowsBuyNew.append(self.popupData[indexPath.row].selectedID.description + "-" + "V")
}
print("after array \(self.arrSelectedRowsBuyNew)")
}
}
return cell
}
for above code o/p: here i need to replace "12-R" with "12-V" and "14-R" with "14-V" here how to check the index and replace the new value
NOTE: here first two digits will be same for old and new array. so can we check and replace with that. first two digits coming from this self.popupData[indexPath.row].selectedID
please do help got stuck here from long time
> after array ["12-R", "14-R", "13-R", "14-V", "12-V"]
EDIT:
before replace array before array ----- ["12-R", "14-R"]
initially all values at index 0
now if i select value from first cell dropdown then its replacing like this
error:
>["12-R", "12-V"]
but i need like ["12-V", "14-R"]
答案1
得分: 1
I'm not sure I fully understand your question but I think you need to replace the line:
self.arrSelectedRowsBuyNew.append(self.popupData[indexPath.row].selectedID.description + "-" + "A")
with:
self.arrSelectedRowsBuyNew[index] = self.popupData[indexPath.row].selectedID.description + "-" + "A"
Your use of append
is adding the new value to the array. If you want to replace a value then simply assign a new value at the desired index.
英文:
I'm not sure I fully understand your question but I think you need to replace the line:
self.arrSelectedRowsBuyNew.append(self.popupData[indexPath.row].selectedID.description + "-" + "A")
with:
self.arrSelectedRowsBuyNew[index] = self.popupData[indexPath.row].selectedID.description + "-" + "A"
Your use of append
is adding the new value to the array. If you want to replace a value then simply assign a new value at the desired index.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论