1

I have the following entry that updates a labels text with the value:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


  UITableViewCell *row = [tableView dequeueReusableCellWithIdentifier:@"TICKET_ITEM"];
  UILabel *itemDiscount = (UILabel *)[row viewWithTag:502];
  itemDiscount.text = [[arrayOfItem objectAtIndex:indexPath.row]TICKET_ITEM_DISCOUNT];

    return row;
}

My problem is that after the fact I have button that allows for setting the discount (which is initially 0). After adjusting a slider I want to be able to take that discount % and update itemDiscount.text with the new value. I figure the way I need to do this is to update the arrayOfItem TICKET_ITEM_DISCOUNT entry and then use reloadData. But how do I update just the single item in the array?

1
  • take mutable array and if you are getting object of dictnaries then make tempDictnary and assign the array object in tempDict and make changes as per requirement and replace the array object with this dictnary Commented Apr 7, 2014 at 3:47

1 Answer 1

4

Check out Apple's documentation on NSArray.

There are a variety of methods that could solve your problem.

indexOfObject: 

or

indexOfObjectPassingTest: 

spring to mind.

To edit an NSArray you'll need to make a mutable copy of the array then assign it back again:

NSMutableArray *temp = [arrayOfItem mutableCopy];
//update your value here
arrayOfItem = temp;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help, just looking a the docs and some other posts I was able to get the following: [[arrayOfItem objectAtIndex:0] setValue:CURRENT_DISCOUNT forKey:@"TICKET_ITEM_DISCOUNT"]; However, that's (obviously) only updating index 0. I'm trying to remember how I had the current row earlier :\
And now I've got it. :) Thanks
Do you want to apply the discount to every object in the array? If so, then you can loop over the array with either a for-loop or for-each loop, and update each item accordingly. Then calling [tableView reloadData]; will update all the cell text labels.

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.