Edit: This post is kinda long so the end question is copied here:
So obviously it is not checking the cell I am clicking, it is checking a
new cell that it is creating. How would I go about checking the cell
that is clicked, and checking the content that is in that specific
cell (i.e. checking the text of that cell’s label).
So in InterfaceBuilder I have a prototype cell, and I do the following code to create a number of cells ina uitableview
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return cellIconNames.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
cellIconName = [cellIconNames objectAtIndex:indexPath.row];
NSString *cellIconImageName = [[self cellIconImages] objectAtIndex:[indexPath row]];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.rightLabel.text = [cellIconNames objectAtIndex:indexPath.row];
cell.carrierImage.image = [UIImage imageNamed:cellIconImageName];
urlString = [cellButtons objectAtIndex:indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 290, 88);
[button setTitle:@"" forState:UIControlStateNormal];
[button addTarget:self action:@selector(startCarrierSite:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
[cell.contentView bringSubviewToFront:button];
return cell;
}
So basically what I did is made an imageview, a label, and a seethrough button on each cell, and made the respective images and labels say different things according to an array.
The button I have in each cell points to the same selector, startCarrierSite:
This is the method for startCarrierSite
-(IBAction)startCarrierSite:(CustomCell *)cell;{
UILabel *rightLabel = (UILabel*) [cell.subviews objectAtIndex:0];
NSString *labelText = rightLabel.text;
if(labelText == @"Aetna"){
NSURL *url = [NSURL URLWithString:@"www.aetna.com"];
[[UIApplication sharedApplication] openURL:url];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
}
}
What I am trying to do is check the label of the cell, and if the .text of that label is equal to a certain string, I run the code for opening safari and blah blah blah, thats not the issue.
The issue is that I can’t seem to check the labels in a cell. The current way I am trying to do this is by running the action with the argument of CustomCell (which is a child of the UITableViewCell class).
This works and the cell is checked when you click it, but then I go to check the text of the label and it crashes. I’m getting the error
Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[__NSArrayI objectAtIndex:]: index 4294967295 beyond bounds for empty array’
So obviously it is not checking the cell I am clicking, it is checking a new cell that it is creating. How would I go about checking the cell that is clicked, and checking the content that is in that specific cell (i.e. checking the text of that cell’s label).
EDIT (ADDING IN UPDATED METHOD):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
if ([cell.urlString isEqualToString:@"Aetna"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.aetna.com"]];
}else if([cell.urlString isEqualToString:nil]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
}
}
Use this UITableViewDelegate method:
From there you can access the cell with
[tableView cellForRowAtIndexPath:indexPath]EDIT
Also, do not compare strings with
yourString == @"someString".Use
[yourString isEqualToString:@"someString"].*EDIT 2 *
Add a property to your CustomCell
@property (nonatomic) NSString *urlString;Set the property when you are creating the cell, and then in the didSelectRowAtIndexPath method, do this;
CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];Now the URL is simply
cell.urlString.