So I am using a uitableview to display multiple images and labels. I want to be able to check the text of the label of the cell that is clicked so that I can identify which one is clicked.
The reason I do this is because I want to do a different action for one cell being clicked, and a different action for another.
This is how I populate my cells (from a prototype cell) With the cell defenition from a class called CustomCell
//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];
cell.urlString = [cellIconNames objectAtIndex:indexPath.row];
return cell;
}
The way I am checking to see which one is clicked is by doing this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
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"]];
}
NSLog(@"cell was clicked %@", cell);
}
When i click a cell, nothing happens. Now i have made that NSLOG so that i can make sure the cells are being read correctly which they are.
I also have commented out my testing line of code, I had the if else statment commented out and just ran the one commented line of code, and nothing happens.
Please help me, i have no idea what is going wrong :/
Try putting the URL method into the string (e.g. http://).