2

I have an array of core data objects called samples, each sample has a depthFrom and depthToo. I load each sample into a tableView to show the depthFrom and Too. I need to check for gaps between the values and if there is, insert a new sample.

The samples in the table could look like below with depthFrom and depthToo,

enter image description here

The issue is since there is a gap between the numbers from 100 to 210 new samples should be added to the table. using a gap of 50 as much as possible so it would look like this with the auto generated samples.

enter image description here

What im unsure of is how to compare the values, i would rather do it as the view loads before cellForRowAtIndexPath is called so i would not need to reload the table again. I was thinking of looping through each value and comparing them but there all in the same array so im not sure how i would do this. I have all the data displaying correctly in my app its just the gaps i need to account for and if im able to find a way to compare the values in the array then i can manage adding in the new objects i just need pointing in the right direction as this is new to me.

If theres anything about my question that is confusing then just add a comment and i will update it accordingly, thanks for any help.

1
  • Looks like a for loop, while keeping tracking of the last depthTo and comparing to the current depthFrom. Can you post how you've already tried to solve this? Commented Jul 20, 2015 at 15:43

1 Answer 1

2

To fix the gaps, you must keep track of the last depthTo and check if there's a gap between it and the current sample. If there is, insert samples with a spacing of 50*, until we reach our current sample.

Here's a pseudocode solution:

samples = NSMutableArray

int lastDepthTo = 0;

for (i = 0; i < [samples count]; i++) {
    s = samples[i]

    // add missing samples (upto current s.depthFrom)
    while (s.depthFrom > lastDepthTo) {

        genDepthTo = MIN(d.depthFrom, lastDepthTo+50)
        generated = new sample(depthFrom: lastDepthTo, depthTo: genDepthTo)
        [samples insert:generated atIndex:i]

        i++ // increment i to skip inserted sample
        lastDepthTo = genDepthTo
    }

    lastDepthTo = s.depthTo
}

Note: this is untested, maybe off by 1 for the indexing of i.

Sign up to request clarification or add additional context in comments.

1 Comment

This was just what i needed, Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.