Search code examples
iphoneuitableviewuiviewtitlebarnsrangeexception

Create a UIView title of a push view using the index of an array


I have a UITableView that pushes to a UIView when the user taps the UITableViewCell. I want to make the title of the push view the text from the cell that the push view is coming from. I am trying to declare an NSUInteger when the row is selected to create a string with objectAtIndex. it is my understanding that objectAtIndex accepts type NSUInteger. This hasn't really been working for me.

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//get index number
NSNumber *selRow = [[NSNumber alloc] initWithInteger:indexPath.row];
NSUInteger rows = (NSUInteger)selRow;
NSLog(@"%@",rows); //this outputs correctly
NSString *pushTitle = [[self.WorkoutListArray objectAtIndex:rows] stringValue];  //error on this line

//declares a new view controller when something is selected -- uses story board to initialize
WorkoutPushViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"WorkoutPush"];

//when selected a new view is pushed
[self.navigationController pushViewController:newView animated:YES];

//adds a title to the push view
newView.title = pushTitle;
}
@end

I am getting an

NSRangeExcpetion error

when I try to declare the string. I've tried a few ways to declare an NSString from the NSArray but haven't had luck yet. I hope my question was clear :)


Solution

  • I forgot that the object I was trying to print had a name and date property. (sorry for making that dumb mistake) The following code does exactly what I wanted it to do.

    NSString *pushTitle = [[self.WorkoutListArray objectAtIndex:indexPath.row] workoutName];
    
    //set the title
    newView.title = pushTitle;
    

    Thanks for the help and patience.