How to make the combination of elements from an array and display on tableView in swift
up vote
1
down vote
favorite
I have an array like this:-
var priceArray = [0, 200, 400, 600, 800, 1000]
I got this data from an Api in my app. And I am using tableView
to Show data So I can provide the selection of prices like this: -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
return cell
}
But the problem is if I will use this:
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
I will get only the first and second value in tableView
But what I actually want is in first row I want to get 0 - 200
then in second row 200 - 400
in third 400 - 600
and so on until I will get the combination of 800 - 1000
. How can I do this. Please help?
ios arrays swift uitableview
add a comment |
up vote
1
down vote
favorite
I have an array like this:-
var priceArray = [0, 200, 400, 600, 800, 1000]
I got this data from an Api in my app. And I am using tableView
to Show data So I can provide the selection of prices like this: -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
return cell
}
But the problem is if I will use this:
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
I will get only the first and second value in tableView
But what I actually want is in first row I want to get 0 - 200
then in second row 200 - 400
in third 400 - 600
and so on until I will get the combination of 800 - 1000
. How can I do this. Please help?
ios arrays swift uitableview
Well, replace the indices0
and1
by something which depends onindexPath.row
...
– Martin R
Nov 13 at 9:47
1
You should havevar priceArray = [[0, 200], [200, 400], etc.
. Note that you can construct it from your currentpriceArray
definition. Then it will be easier,let values = priceArray[indexPath.row]; cell.priceLabel.text = "(values[0]) - (values[1])"
– Larme
Nov 13 at 9:48
But Sir @Larme don't it be a long process ?
– wings
Nov 13 at 9:50
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an array like this:-
var priceArray = [0, 200, 400, 600, 800, 1000]
I got this data from an Api in my app. And I am using tableView
to Show data So I can provide the selection of prices like this: -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
return cell
}
But the problem is if I will use this:
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
I will get only the first and second value in tableView
But what I actually want is in first row I want to get 0 - 200
then in second row 200 - 400
in third 400 - 600
and so on until I will get the combination of 800 - 1000
. How can I do this. Please help?
ios arrays swift uitableview
I have an array like this:-
var priceArray = [0, 200, 400, 600, 800, 1000]
I got this data from an Api in my app. And I am using tableView
to Show data So I can provide the selection of prices like this: -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
return cell
}
But the problem is if I will use this:
cell.priceLabel.text = "(priceArray[0]) - (priceArray[1])"
I will get only the first and second value in tableView
But what I actually want is in first row I want to get 0 - 200
then in second row 200 - 400
in third 400 - 600
and so on until I will get the combination of 800 - 1000
. How can I do this. Please help?
ios arrays swift uitableview
ios arrays swift uitableview
edited Nov 13 at 9:50
Dávid Pásztor
19.4k72547
19.4k72547
asked Nov 13 at 9:44
wings
912421
912421
Well, replace the indices0
and1
by something which depends onindexPath.row
...
– Martin R
Nov 13 at 9:47
1
You should havevar priceArray = [[0, 200], [200, 400], etc.
. Note that you can construct it from your currentpriceArray
definition. Then it will be easier,let values = priceArray[indexPath.row]; cell.priceLabel.text = "(values[0]) - (values[1])"
– Larme
Nov 13 at 9:48
But Sir @Larme don't it be a long process ?
– wings
Nov 13 at 9:50
add a comment |
Well, replace the indices0
and1
by something which depends onindexPath.row
...
– Martin R
Nov 13 at 9:47
1
You should havevar priceArray = [[0, 200], [200, 400], etc.
. Note that you can construct it from your currentpriceArray
definition. Then it will be easier,let values = priceArray[indexPath.row]; cell.priceLabel.text = "(values[0]) - (values[1])"
– Larme
Nov 13 at 9:48
But Sir @Larme don't it be a long process ?
– wings
Nov 13 at 9:50
Well, replace the indices
0
and 1
by something which depends on indexPath.row
...– Martin R
Nov 13 at 9:47
Well, replace the indices
0
and 1
by something which depends on indexPath.row
...– Martin R
Nov 13 at 9:47
1
1
You should have
var priceArray = [[0, 200], [200, 400], etc.
. Note that you can construct it from your current priceArray
definition. Then it will be easier, let values = priceArray[indexPath.row]; cell.priceLabel.text = "(values[0]) - (values[1])"
– Larme
Nov 13 at 9:48
You should have
var priceArray = [[0, 200], [200, 400], etc.
. Note that you can construct it from your current priceArray
definition. Then it will be easier, let values = priceArray[indexPath.row]; cell.priceLabel.text = "(values[0]) - (values[1])"
– Larme
Nov 13 at 9:48
But Sir @Larme don't it be a long process ?
– wings
Nov 13 at 9:50
But Sir @Larme don't it be a long process ?
– wings
Nov 13 at 9:50
add a comment |
6 Answers
6
active
oldest
votes
up vote
1
down vote
accepted
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (priceArray.count - 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
let index = indexPath.row
cell.priceLabel.text = "(priceArray[index]) - (priceArray[index + 1])"
return cell
}
This should do the job.
because on reaching the last element, code will crash as(last + 1)
will not be present
– Aakash
Nov 13 at 9:55
add a comment |
up vote
2
down vote
You need to use indexPath.row
to index priceArray
in cellForRowAt
and also need to modify numberOfRowsInSection
since there are less price ranges than the number of elements in priceArray
.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
WhypriceArray.count - 2
?
– Martin R
Nov 13 at 9:55
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
Should bepriceArray.count - 1
– Aakash
Nov 13 at 9:59
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
|
show 1 more comment
up vote
1
down vote
Your price array has 6 elements. but you need 5 price range.
So you need to minus 1 from numberOfRowsInSection, that will give you 5 element.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
add a comment |
up vote
1
down vote
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row + 1])"
return cell
}
1
This code will not work as the cell is not being returned when theif
case fails, it's a compile time error.
– Aakash
Nov 13 at 9:57
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
Also for the updated answer the last cell'spriceLabel
label text will not be set, this will not be desirable behaviour.
– Aakash
Nov 13 at 10:02
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
add a comment |
up vote
1
down vote
You can simply create function for creating dataSource array for your tableView:
func makeTableDataSource(from priceArray: [Int]) -> [String] {
var resultArray = [String]()
for (index, price) in priceArray.enumerated() where index < priceArray.count - 1 {
resultArray.append("(price) - (priceArray [index + 1])")
}
return resultArray
}
Results of it:
var priceArray = [0, 200, 400, 600, 800, 1000]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
- 2 : "400 - 600"
- 3 : "600 - 800"
- 4 : "800 - 1000"
var priceArray = [0, 200, 400]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
var priceArray = [0]
Result:
After that, just create property like private var tableViewDataSource = [String]()
Then in your viewDidLoad()
add:
override func viewDidLoad() {
super.viewDidLoad()
var priceArray = [0, 200, 400, 600, 800, 1000]
tableViewDataSource = makeTableDataSource(from: priceArray)
}
And it's easy to use in your case:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewDataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = tableViewDataSource[indexPath.row]
return cell
}
Pros of this approach:
- you create your data source only once when open viewController
- datasource for your cell not generating "on the fly"
- single responsibility for generating tableDataSource data (if you'll need to update strings, you need todo it only in
makeTableDataSource
, and everything else work fine) - potentially errors easy to locate
- single "data of trues" for all table (you don't need use
array.count - 1
, orarray.count + 1
in different cases, so less places for potentially crashes) - and you can reuse this function of course :)
Happy coding!
Thanks for the answer
– wings
Nov 13 at 11:28
add a comment |
up vote
1
down vote
All you need is to prepare dataSource properly.
To do that, I recommend to split existing price array into chunks and map it to objects you want to represent. In your case String type.
You can use following extension:
extension Sequence {
func split(by chunkSize: Int) -> [[Self.Element]] {
return self.reduce(into:) { memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clump {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
}
}
Usage:
var priceArray = [0, 200, 400, 600, 800, 1000]
let dataSource = priceArray.split(by: 2).map { "($0[0]) - ($0[1])" }
// result: ["0 - 200", "400 - 600", "800 - 1000"]
thanks for the contribution
– wings
Nov 13 at 11:32
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (priceArray.count - 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
let index = indexPath.row
cell.priceLabel.text = "(priceArray[index]) - (priceArray[index + 1])"
return cell
}
This should do the job.
because on reaching the last element, code will crash as(last + 1)
will not be present
– Aakash
Nov 13 at 9:55
add a comment |
up vote
1
down vote
accepted
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (priceArray.count - 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
let index = indexPath.row
cell.priceLabel.text = "(priceArray[index]) - (priceArray[index + 1])"
return cell
}
This should do the job.
because on reaching the last element, code will crash as(last + 1)
will not be present
– Aakash
Nov 13 at 9:55
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (priceArray.count - 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
let index = indexPath.row
cell.priceLabel.text = "(priceArray[index]) - (priceArray[index + 1])"
return cell
}
This should do the job.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (priceArray.count - 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
let index = indexPath.row
cell.priceLabel.text = "(priceArray[index]) - (priceArray[index + 1])"
return cell
}
This should do the job.
answered Nov 13 at 9:53
Aakash
1,344715
1,344715
because on reaching the last element, code will crash as(last + 1)
will not be present
– Aakash
Nov 13 at 9:55
add a comment |
because on reaching the last element, code will crash as(last + 1)
will not be present
– Aakash
Nov 13 at 9:55
because on reaching the last element, code will crash as
(last + 1)
will not be present– Aakash
Nov 13 at 9:55
because on reaching the last element, code will crash as
(last + 1)
will not be present– Aakash
Nov 13 at 9:55
add a comment |
up vote
2
down vote
You need to use indexPath.row
to index priceArray
in cellForRowAt
and also need to modify numberOfRowsInSection
since there are less price ranges than the number of elements in priceArray
.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
WhypriceArray.count - 2
?
– Martin R
Nov 13 at 9:55
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
Should bepriceArray.count - 1
– Aakash
Nov 13 at 9:59
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
|
show 1 more comment
up vote
2
down vote
You need to use indexPath.row
to index priceArray
in cellForRowAt
and also need to modify numberOfRowsInSection
since there are less price ranges than the number of elements in priceArray
.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
WhypriceArray.count - 2
?
– Martin R
Nov 13 at 9:55
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
Should bepriceArray.count - 1
– Aakash
Nov 13 at 9:59
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
You need to use indexPath.row
to index priceArray
in cellForRowAt
and also need to modify numberOfRowsInSection
since there are less price ranges than the number of elements in priceArray
.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
You need to use indexPath.row
to index priceArray
in cellForRowAt
and also need to modify numberOfRowsInSection
since there are less price ranges than the number of elements in priceArray
.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
edited Nov 13 at 10:00
answered Nov 13 at 9:51
Dávid Pásztor
19.4k72547
19.4k72547
WhypriceArray.count - 2
?
– Martin R
Nov 13 at 9:55
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
Should bepriceArray.count - 1
– Aakash
Nov 13 at 9:59
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
|
show 1 more comment
WhypriceArray.count - 2
?
– Martin R
Nov 13 at 9:55
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
Should bepriceArray.count - 1
– Aakash
Nov 13 at 9:59
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
Why
priceArray.count - 2
?– Martin R
Nov 13 at 9:55
Why
priceArray.count - 2
?– Martin R
Nov 13 at 9:55
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
we should use return priceArray.count - 1 because we are not used only one value 1000 as first value
– Jatin Kathrotiya
Nov 13 at 9:59
Should be
priceArray.count - 1
– Aakash
Nov 13 at 9:59
Should be
priceArray.count - 1
– Aakash
Nov 13 at 9:59
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Right, updated my answer
– Dávid Pásztor
Nov 13 at 10:01
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
Perfect now we have 3 similar answers :P
– Aakash
Nov 13 at 10:03
|
show 1 more comment
up vote
1
down vote
Your price array has 6 elements. but you need 5 price range.
So you need to minus 1 from numberOfRowsInSection, that will give you 5 element.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
add a comment |
up vote
1
down vote
Your price array has 6 elements. but you need 5 price range.
So you need to minus 1 from numberOfRowsInSection, that will give you 5 element.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Your price array has 6 elements. but you need 5 price range.
So you need to minus 1 from numberOfRowsInSection, that will give you 5 element.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
Your price array has 6 elements. but you need 5 price range.
So you need to minus 1 from numberOfRowsInSection, that will give you 5 element.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row+1])"
return cell
}
answered Nov 13 at 10:00
Viren Malhan
424
424
add a comment |
add a comment |
up vote
1
down vote
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row + 1])"
return cell
}
1
This code will not work as the cell is not being returned when theif
case fails, it's a compile time error.
– Aakash
Nov 13 at 9:57
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
Also for the updated answer the last cell'spriceLabel
label text will not be set, this will not be desirable behaviour.
– Aakash
Nov 13 at 10:02
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
add a comment |
up vote
1
down vote
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row + 1])"
return cell
}
1
This code will not work as the cell is not being returned when theif
case fails, it's a compile time error.
– Aakash
Nov 13 at 9:57
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
Also for the updated answer the last cell'spriceLabel
label text will not be set, this will not be desirable behaviour.
– Aakash
Nov 13 at 10:02
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
add a comment |
up vote
1
down vote
up vote
1
down vote
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row + 1])"
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return priceArray.count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = "(priceArray[indexPath.row]) - (priceArray[indexPath.row + 1])"
return cell
}
edited Nov 13 at 10:10
answered Nov 13 at 9:54
Prashant Dabhi
113
113
1
This code will not work as the cell is not being returned when theif
case fails, it's a compile time error.
– Aakash
Nov 13 at 9:57
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
Also for the updated answer the last cell'spriceLabel
label text will not be set, this will not be desirable behaviour.
– Aakash
Nov 13 at 10:02
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
add a comment |
1
This code will not work as the cell is not being returned when theif
case fails, it's a compile time error.
– Aakash
Nov 13 at 9:57
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
Also for the updated answer the last cell'spriceLabel
label text will not be set, this will not be desirable behaviour.
– Aakash
Nov 13 at 10:02
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
1
1
This code will not work as the cell is not being returned when the
if
case fails, it's a compile time error.– Aakash
Nov 13 at 9:57
This code will not work as the cell is not being returned when the
if
case fails, it's a compile time error.– Aakash
Nov 13 at 9:57
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
sorry for my mistake, please check my edited answer. @Aakash
– Prashant Dabhi
Nov 13 at 10:00
Also for the updated answer the last cell's
priceLabel
label text will not be set, this will not be desirable behaviour.– Aakash
Nov 13 at 10:02
Also for the updated answer the last cell's
priceLabel
label text will not be set, this will not be desirable behaviour.– Aakash
Nov 13 at 10:02
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
Yes, the updated answer is now correct.
– Aakash
Nov 13 at 10:12
add a comment |
up vote
1
down vote
You can simply create function for creating dataSource array for your tableView:
func makeTableDataSource(from priceArray: [Int]) -> [String] {
var resultArray = [String]()
for (index, price) in priceArray.enumerated() where index < priceArray.count - 1 {
resultArray.append("(price) - (priceArray [index + 1])")
}
return resultArray
}
Results of it:
var priceArray = [0, 200, 400, 600, 800, 1000]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
- 2 : "400 - 600"
- 3 : "600 - 800"
- 4 : "800 - 1000"
var priceArray = [0, 200, 400]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
var priceArray = [0]
Result:
After that, just create property like private var tableViewDataSource = [String]()
Then in your viewDidLoad()
add:
override func viewDidLoad() {
super.viewDidLoad()
var priceArray = [0, 200, 400, 600, 800, 1000]
tableViewDataSource = makeTableDataSource(from: priceArray)
}
And it's easy to use in your case:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewDataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = tableViewDataSource[indexPath.row]
return cell
}
Pros of this approach:
- you create your data source only once when open viewController
- datasource for your cell not generating "on the fly"
- single responsibility for generating tableDataSource data (if you'll need to update strings, you need todo it only in
makeTableDataSource
, and everything else work fine) - potentially errors easy to locate
- single "data of trues" for all table (you don't need use
array.count - 1
, orarray.count + 1
in different cases, so less places for potentially crashes) - and you can reuse this function of course :)
Happy coding!
Thanks for the answer
– wings
Nov 13 at 11:28
add a comment |
up vote
1
down vote
You can simply create function for creating dataSource array for your tableView:
func makeTableDataSource(from priceArray: [Int]) -> [String] {
var resultArray = [String]()
for (index, price) in priceArray.enumerated() where index < priceArray.count - 1 {
resultArray.append("(price) - (priceArray [index + 1])")
}
return resultArray
}
Results of it:
var priceArray = [0, 200, 400, 600, 800, 1000]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
- 2 : "400 - 600"
- 3 : "600 - 800"
- 4 : "800 - 1000"
var priceArray = [0, 200, 400]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
var priceArray = [0]
Result:
After that, just create property like private var tableViewDataSource = [String]()
Then in your viewDidLoad()
add:
override func viewDidLoad() {
super.viewDidLoad()
var priceArray = [0, 200, 400, 600, 800, 1000]
tableViewDataSource = makeTableDataSource(from: priceArray)
}
And it's easy to use in your case:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewDataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = tableViewDataSource[indexPath.row]
return cell
}
Pros of this approach:
- you create your data source only once when open viewController
- datasource for your cell not generating "on the fly"
- single responsibility for generating tableDataSource data (if you'll need to update strings, you need todo it only in
makeTableDataSource
, and everything else work fine) - potentially errors easy to locate
- single "data of trues" for all table (you don't need use
array.count - 1
, orarray.count + 1
in different cases, so less places for potentially crashes) - and you can reuse this function of course :)
Happy coding!
Thanks for the answer
– wings
Nov 13 at 11:28
add a comment |
up vote
1
down vote
up vote
1
down vote
You can simply create function for creating dataSource array for your tableView:
func makeTableDataSource(from priceArray: [Int]) -> [String] {
var resultArray = [String]()
for (index, price) in priceArray.enumerated() where index < priceArray.count - 1 {
resultArray.append("(price) - (priceArray [index + 1])")
}
return resultArray
}
Results of it:
var priceArray = [0, 200, 400, 600, 800, 1000]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
- 2 : "400 - 600"
- 3 : "600 - 800"
- 4 : "800 - 1000"
var priceArray = [0, 200, 400]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
var priceArray = [0]
Result:
After that, just create property like private var tableViewDataSource = [String]()
Then in your viewDidLoad()
add:
override func viewDidLoad() {
super.viewDidLoad()
var priceArray = [0, 200, 400, 600, 800, 1000]
tableViewDataSource = makeTableDataSource(from: priceArray)
}
And it's easy to use in your case:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewDataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = tableViewDataSource[indexPath.row]
return cell
}
Pros of this approach:
- you create your data source only once when open viewController
- datasource for your cell not generating "on the fly"
- single responsibility for generating tableDataSource data (if you'll need to update strings, you need todo it only in
makeTableDataSource
, and everything else work fine) - potentially errors easy to locate
- single "data of trues" for all table (you don't need use
array.count - 1
, orarray.count + 1
in different cases, so less places for potentially crashes) - and you can reuse this function of course :)
Happy coding!
You can simply create function for creating dataSource array for your tableView:
func makeTableDataSource(from priceArray: [Int]) -> [String] {
var resultArray = [String]()
for (index, price) in priceArray.enumerated() where index < priceArray.count - 1 {
resultArray.append("(price) - (priceArray [index + 1])")
}
return resultArray
}
Results of it:
var priceArray = [0, 200, 400, 600, 800, 1000]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
- 2 : "400 - 600"
- 3 : "600 - 800"
- 4 : "800 - 1000"
var priceArray = [0, 200, 400]
Result:
- 0 : "0 - 200"
- 1 : "200 - 400"
var priceArray = [0]
Result:
After that, just create property like private var tableViewDataSource = [String]()
Then in your viewDidLoad()
add:
override func viewDidLoad() {
super.viewDidLoad()
var priceArray = [0, 200, 400, 600, 800, 1000]
tableViewDataSource = makeTableDataSource(from: priceArray)
}
And it's easy to use in your case:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewDataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
cell.priceLabel.text = tableViewDataSource[indexPath.row]
return cell
}
Pros of this approach:
- you create your data source only once when open viewController
- datasource for your cell not generating "on the fly"
- single responsibility for generating tableDataSource data (if you'll need to update strings, you need todo it only in
makeTableDataSource
, and everything else work fine) - potentially errors easy to locate
- single "data of trues" for all table (you don't need use
array.count - 1
, orarray.count + 1
in different cases, so less places for potentially crashes) - and you can reuse this function of course :)
Happy coding!
answered Nov 13 at 11:17
rubik
144112
144112
Thanks for the answer
– wings
Nov 13 at 11:28
add a comment |
Thanks for the answer
– wings
Nov 13 at 11:28
Thanks for the answer
– wings
Nov 13 at 11:28
Thanks for the answer
– wings
Nov 13 at 11:28
add a comment |
up vote
1
down vote
All you need is to prepare dataSource properly.
To do that, I recommend to split existing price array into chunks and map it to objects you want to represent. In your case String type.
You can use following extension:
extension Sequence {
func split(by chunkSize: Int) -> [[Self.Element]] {
return self.reduce(into:) { memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clump {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
}
}
Usage:
var priceArray = [0, 200, 400, 600, 800, 1000]
let dataSource = priceArray.split(by: 2).map { "($0[0]) - ($0[1])" }
// result: ["0 - 200", "400 - 600", "800 - 1000"]
thanks for the contribution
– wings
Nov 13 at 11:32
add a comment |
up vote
1
down vote
All you need is to prepare dataSource properly.
To do that, I recommend to split existing price array into chunks and map it to objects you want to represent. In your case String type.
You can use following extension:
extension Sequence {
func split(by chunkSize: Int) -> [[Self.Element]] {
return self.reduce(into:) { memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clump {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
}
}
Usage:
var priceArray = [0, 200, 400, 600, 800, 1000]
let dataSource = priceArray.split(by: 2).map { "($0[0]) - ($0[1])" }
// result: ["0 - 200", "400 - 600", "800 - 1000"]
thanks for the contribution
– wings
Nov 13 at 11:32
add a comment |
up vote
1
down vote
up vote
1
down vote
All you need is to prepare dataSource properly.
To do that, I recommend to split existing price array into chunks and map it to objects you want to represent. In your case String type.
You can use following extension:
extension Sequence {
func split(by chunkSize: Int) -> [[Self.Element]] {
return self.reduce(into:) { memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clump {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
}
}
Usage:
var priceArray = [0, 200, 400, 600, 800, 1000]
let dataSource = priceArray.split(by: 2).map { "($0[0]) - ($0[1])" }
// result: ["0 - 200", "400 - 600", "800 - 1000"]
All you need is to prepare dataSource properly.
To do that, I recommend to split existing price array into chunks and map it to objects you want to represent. In your case String type.
You can use following extension:
extension Sequence {
func split(by chunkSize: Int) -> [[Self.Element]] {
return self.reduce(into:) { memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clump {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
}
}
Usage:
var priceArray = [0, 200, 400, 600, 800, 1000]
let dataSource = priceArray.split(by: 2).map { "($0[0]) - ($0[1])" }
// result: ["0 - 200", "400 - 600", "800 - 1000"]
answered Nov 13 at 11:29
Stanislau Baranouski
503615
503615
thanks for the contribution
– wings
Nov 13 at 11:32
add a comment |
thanks for the contribution
– wings
Nov 13 at 11:32
thanks for the contribution
– wings
Nov 13 at 11:32
thanks for the contribution
– wings
Nov 13 at 11:32
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278083%2fhow-to-make-the-combination-of-elements-from-an-array-and-display-on-tableview-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well, replace the indices
0
and1
by something which depends onindexPath.row
...– Martin R
Nov 13 at 9:47
1
You should have
var priceArray = [[0, 200], [200, 400], etc.
. Note that you can construct it from your currentpriceArray
definition. Then it will be easier,let values = priceArray[indexPath.row]; cell.priceLabel.text = "(values[0]) - (values[1])"
– Larme
Nov 13 at 9:48
But Sir @Larme don't it be a long process ?
– wings
Nov 13 at 9:50