Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Using coreData and adding (math word) the previous row with the new row?

tripp13tripp13 Posts: 148Registered Users
I'm nearing the end of my first app and hope it does well. I think it's put together well, but one of my last steps deals with a fairly complex algorithm that calculates each time the user updates and provides the new values once an action is done. I'm using core data and actually like it a lot. I was dreading it initially based on a lot of the posts I read, but it's grown on me. I digress.

So, each time the user enters a new value, that value is displayed in the tableView. since the user can theoretically enter an infinite amount of rows, hard coding is out of the question. I created a bunch of labels and textfields in a different project with the same premise to test the algorithm for correctness and it checked out.

So, could somebody help me out a little and explain / point me in a direction to add tableviewcells together. for example:

(The right side of the = are integers...I know what they really are, but just simplifying for the sake of the example and so someone can understand what i'm attempting to do. This is a mathematical issue.)

indexPath.row 0 = 1
indexPath.row 1 = 2
indexPath.row 2 = 3

The answer i would want would be 6...1 + 2 = 3...3 + 3 = 6.

I really do appreciate the guidance. Please let me know if you need any clarification. Thanks, y'all.
Post edited by tripp13 on

Replies

  • Meredi86Meredi86 Posts: 1,108Registered Users
    i have a similar method in my app. I have coredata that stores -well data :P -and one attribute is a numeric value (i think it is actually stored as a string but thats no issue here). What i needed to do, was once the user had input their items (similar to yours - it could be 1 it could be 1000+) and gone onto the next view the numbers would all be added together and the total displayed on screen.

    I did this taking a different approach to what you seem to be looking at at the moment. Instead of adding what is in the tableview i have added what is in the CoreData attribute list.

    So the way i went around this was something similar to this:

    fetch an array from my core data entity
    use the "count" method on that array to find out how many numbers i will have
    loop up my count and during this loop:
    assign a "current object" (i.e. set a variable "currentObject" to the current array index)
    assign a "singleObject" (i.e. now i have my "currentObject" (an array of items from one coredata row) assign the "valueForKey" to "singleObject")
    --- In my case convert the current number (formatted as NSString) to a floatValue (i need decimals)
    Add the current number to a new array
    So by this point i have taken out all of my required numbers from my coredata entity, then assigned them to a new array, leaving me with only an array of floats. Next:

    use another for loop to add up the numbers in the array. my loop here looks similar to this:

    for (number in array) {
    float aFloat = number
    sum = sum + aFloat
    }

    At this point "sum" should contain the total value of all the numbers in the array added together.

    Hope i have explained clearly and that it helps you out
  • tripp13tripp13 Posts: 148Registered Users
    Meredi, thanks a bunch for the time you put in. Can't wait to test it out when I get home.

    I Figured I'd be pulling from the data model, but didn't quite know how to call that data for the individual rows that data is saved in. I do appreciate it.

    I was wondering if this way might work and would like your opinion if you have the time.

    Could I setup a separate Formulas entity with a fetch request or relationship that sucks the needed values from the firstTableView's Model and another relationship to the secondTableView's Model and base the calculations off of that? Just thinking right now.

    One last question, I think I answered it for myself, I can't have the previous totals change and need them to stay constant unless a previous (not a current and not a future) value is changed by the user. I would just set a sum object for each entity that is created, right?

    Thanks again. What you said makes sense and is fairly obvious once you said it. Thank you.
Sign In or Register to comment.