如何在Swift中将数组中的值替换为相同索引处的新值。

huangapple go评论71阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月11日 02:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定