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?










share|improve this question
























  • Well, replace the indices 0 and 1 by something which depends on indexPath.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 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















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?










share|improve this question
























  • Well, replace the indices 0 and 1 by something which depends on indexPath.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 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 indices 0 and 1 by something which depends on indexPath.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 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


















  • Well, replace the indices 0 and 1 by something which depends on indexPath.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 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
















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












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.






share|improve this answer





















  • because on reaching the last element, code will crash as (last + 1) will not be present
    – Aakash
    Nov 13 at 9:55


















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
}





share|improve this answer























  • 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










  • Should be priceArray.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


















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
}





share|improve this answer




























    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
    }





    share|improve this answer



















    • 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












    • 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










    • Yes, the updated answer is now correct.
      – Aakash
      Nov 13 at 10:12




















    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, or array.count + 1 in different cases, so less places for potentially crashes)

    • and you can reuse this function of course :)


    Happy coding!






    share|improve this answer





















    • Thanks for the answer
      – wings
      Nov 13 at 11:28


















    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"]





    share|improve this answer





















    • thanks for the contribution
      – wings
      Nov 13 at 11:32











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer





















    • because on reaching the last element, code will crash as (last + 1) will not be present
      – Aakash
      Nov 13 at 9:55















    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.






    share|improve this answer





















    • because on reaching the last element, code will crash as (last + 1) will not be present
      – Aakash
      Nov 13 at 9:55













    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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
    }





    share|improve this answer























    • 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










    • Should be priceArray.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















    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
    }





    share|improve this answer























    • 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










    • Should be priceArray.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













    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
    }





    share|improve this answer














    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
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 at 10:00

























    answered Nov 13 at 9:51









    Dávid Pásztor

    19.4k72547




    19.4k72547












    • 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










    • Should be priceArray.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










    • 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










    • 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










    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
    }





    share|improve this answer

























      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
      }





      share|improve this answer























        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
        }





        share|improve this answer












        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
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 10:00









        Viren Malhan

        424




        424






















            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
            }





            share|improve this answer



















            • 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












            • 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










            • Yes, the updated answer is now correct.
              – Aakash
              Nov 13 at 10:12

















            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
            }





            share|improve this answer



















            • 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












            • 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










            • Yes, the updated answer is now correct.
              – Aakash
              Nov 13 at 10:12















            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
            }





            share|improve this answer














            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
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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










            • 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
















            • 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












            • 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










            • 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












            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, or array.count + 1 in different cases, so less places for potentially crashes)

            • and you can reuse this function of course :)


            Happy coding!






            share|improve this answer





















            • Thanks for the answer
              – wings
              Nov 13 at 11:28















            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, or array.count + 1 in different cases, so less places for potentially crashes)

            • and you can reuse this function of course :)


            Happy coding!






            share|improve this answer





















            • Thanks for the answer
              – wings
              Nov 13 at 11:28













            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, or array.count + 1 in different cases, so less places for potentially crashes)

            • and you can reuse this function of course :)


            Happy coding!






            share|improve this answer












            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, or array.count + 1 in different cases, so less places for potentially crashes)

            • and you can reuse this function of course :)


            Happy coding!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 at 11:17









            rubik

            144112




            144112












            • 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




            Thanks for the answer
            – wings
            Nov 13 at 11:28










            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"]





            share|improve this answer





















            • thanks for the contribution
              – wings
              Nov 13 at 11:32















            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"]





            share|improve this answer





















            • thanks for the contribution
              – wings
              Nov 13 at 11:32













            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"]





            share|improve this answer












            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"]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 at 11:29









            Stanislau Baranouski

            503615




            503615












            • 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




            thanks for the contribution
            – wings
            Nov 13 at 11:32


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents