0

hope you're having a good day,

What's the best way to set each button in a tableview cell to a different value based on a NSMutableArray value?

I can get the values into the array and NSlog and it displays all of the Urls fine in the xcode console. When I click on the button in my app it is only loading the last result a string was set to from the array for all of the buttons in the tableview.

For example when I tap the button code below it will only load the last string value (I want to change this to each value from an array of results), I understand this is how strings should work since as it iterates over the results it will change the string but how do I set the button code below to load each different URL in the Array rather than the string value?

Button code is:

NSLog(@"detailsB %@", self.detailsB); //detailsB is a string value and on NSlog it will only show the last Url from the results  

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.detailsB]];  //sets all buttons as the last string value and it opens Safari fine with that one url

Thanks all, any help is very much appreciated,

Gerard

2
  • Your question is not very clear! do you want that when you tap on a button it should load get all urls from array and open them in to Safari...? Commented Jan 19, 2014 at 12:11
  • Hi C_X, Yes it should set the value for each button so when you click on each button it will open the relevant url in Safari on the device. Currently it is only opened the last url which the string is set to. How would I go about setting the button value to the indexPath.row and opening each url for each cell? Thank you. Commented Jan 20, 2014 at 2:11

2 Answers 2

1

If you want your button code to load an URL from an array, using the current cell's row as the index into the array, then you actually have to load the URL from the array. Your code is always using self.detailsB, which I assume is a single, fixed URL.

First you need to figure out which cell's button was pressed. Then you use need to use the cell's indexPath.row to index into your array of URLS and fetch the appropriate URL.

Which part are you having problems with?

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

2 Comments

Hi Duncan, I can get the urls into the array and set the string to the url from the array and it runs through all of the cells and outputs the correct urls in NSlog. I just can't find a way to set each button to the value for the cell, they all get set to the final string value rather than each object url in the array. How would you go about using indexPath.row to set the string to each object? Or would you remove the string entirely and use the object in the Array directly? Thanks Duncan.
I would not try to set the string in the button. Have a single action that each button invokes. In that action, figure out the indexPath.row of that button. You can figure that out by putting a tag on each button that is it's row number, or you can figure it out using the coordinates of the button. See my second answer below for code to do that:
0

Create a single IBAction method for the buttons in all your cells. That action method would first figure out which indexPath the button belongs to, then look up the URL for that indexPath from your array.

- (IBAction) cellButtonTapped: (UIButton *) sender;
{
  //convert the button's center coordinates to the table view's coordinate system.
  CGPoint viewCenter = [self.tableview convertPoint: sender.center 
    fromView: sender.superview];

  //Ask the table view which cell's index path contains that point.
  NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint: viewCenter];
  int row =  indexPath.row;

  //Now fetch the URL string for that row.
  NSString *theURLString = theURLSArray[row];

  //create an NSURL object from the string.
  NSURL *theURL = [NSURL URLWithString: theURLString];

  //Finally, invoke the URL.
  [[UIApplication sharedApplication] openURL: theURL];  
}

Comments

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.