Reputation: 3822
I have a UITableView
that I added to a UIView
inside the main view. The table loads a custom cell and loads the data and responds to scrolling properly, but didSelectRowAtIndexPath
is not being called. If I move the table outside its container UIView
, the delegate
method gets called.
So it only doesn't work when inside the container view, but that view has userInteractionEnable = YES
and the table responds to scrolling.
The table is also set to single selection.
Any idea why it won't respond to selection? Thanks in advance for any help
Here is some code:
self.table.delegate = self;
self.table.dataSource = self;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.classNames count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ClassCell" owner:self options:nil];
cell = self.tvCell;
self.tvCell = nil;
}
//Set up custom cell
return cell;
}
//When cell is tapped
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *className = [NSString stringWithFormat:@"%@", [self.classNames objectAtIndex:indexPath.row]];
if([[AppManager sharedManager] isPad])
{
ClassWork *controller = [[ClassWork alloc] initWithNibName:@"ClassWork_iPad" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
controller.delegate = self;
[controller setClassIndex:indexPath.row];
[controller setClassTitle:[NSString stringWithString:className]];
[self presentModalViewController:controller animated:YES];
}
else
{
if([[AppManager sharedManager] is4inchScreen])
{
ClassWork *controller = [[ClassWork alloc] initWithNibName:@"ClassWork_iPhone4in" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
controller.delegate = self;
[controller setClassIndex:indexPath.row];
[controller setClassTitle:[NSString stringWithString:className]];
[self presentModalViewController:controller animated:YES];
}
else
{
ClassWork *controller = [[ClassWork alloc] initWithNibName:@"ClassWork_iPhone" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
controller.delegate = self;
[controller setClassIndex:indexPath.row];
[controller setClassTitle:[NSString stringWithString:className]];
[self presentModalViewController:controller animated:YES];
}
}
}
//Delete Cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If row is deleted, remove it from the list.
if(editingStyle == UITableViewCellEditingStyleDelete)
{
[self.classNames removeObjectAtIndex:indexPath.row];
[self.classDays removeObjectAtIndex:indexPath.row];
[self.classTimes removeObjectAtIndex:indexPath.row];
//Get Corresponding File Path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:(0)];
NSString *classNum = [NSString stringWithFormat:@"Class%d", indexPath.row];
NSString *classFolder = [documentsDirectory stringByAppendingPathComponent:classNum];
//Remove Folder
[[NSFileManager defaultManager] removeItemAtPath: classFolder error: nil];
//Change numClasses
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:kNumClasses];
//Get current number
NSString *str = [[AppManager sharedManager] loadFileData:fileName];
int numClasses = [str intValue] - 1;
//Decrease by 1
str = [NSString stringWithFormat:@"%d", numClasses];
//Re-write
NSData *outFileData = [str dataUsingEncoding:[NSString defaultCStringEncoding]];
[[AppManager sharedManager] writeFileData:fileName :outFileData];
//Rename class folders
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *newFilePath;
NSString *newClassNum;
int newIndex = 0;
for(int i = 0; i < (numClasses + 1); i++)
{
classNum = [NSString stringWithFormat:@"Class%d", i];
classFolder = [documentsDirectory stringByAppendingPathComponent:classNum];
if([[NSFileManager defaultManager] fileExistsAtPath:classFolder])
{
newClassNum = [NSString stringWithFormat:@"Class%d", newIndex];
newFilePath = [documentsDirectory stringByAppendingPathComponent:newClassNum];
newIndex++;
if(classFolder != newFilePath)
{
// Rename the file by moving the file
[fileMgr moveItemAtPath:classFolder toPath:newFilePath error:nil];
}
}
}
//Reload Table
[self.table reloadData];
[self setNumberOfAssignmentsToCome];
if(numClasses == 0)
{
self.noClassesLabel1.hidden = NO;
self.noClassesLabel2.hidden = NO;
self.table.userInteractionEnabled = NO;
}
}
}
Upvotes: 5
Views: 3473
Reputation: 3822
Turns out that there was a gesture recognizer sucking up all the touches. I changed it to be there at certain times and to be gone others, and now the table view is working properly
Upvotes: 18