Reputation: 1717
This is my first question about TableView
I have a view for UITableViewController and a view for UITableViewCell. I've created a different CellView design for UITableViewController.
My cell view is working fine. I've a small issue that once I click on the btnEdit
it won't stop the UITableViewController from scrolling. Please how can I solve this issue?
posListViewController.h
//UPDATED
@interface posListViewController : UITableViewController <CellHandlingDelegate> //ISSUE{
}
@property (nonatomic, retain) IBOutlet UITableView *tableV;
//UPDATED
-(void) buttonWasSelected:(id)sender;
posListViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PosListViewCell *cell = (PosListViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PosListViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[PosListViewCell class]]){
cell = (PosListViewCell *)currentObject;
break;
}
}
}
NSDictionary *selectedContent = [data objectAtIndex:indexPath.row];
[cell setData:selectedContent];
return cell;
}
//UPDATED
- (void) buttonWasSelected:(id)sender{
self.tableV.scrollEnabled = NO;
}
PosListViewCell.h
#import <UIKit/UIKit.h>
//UPDATED
@protocol CellHandlingDelegate <NSObject>
- (void) buttonWasSelected:(id)sender;
@end
@class posListViewController;
@interface posListViewCell : UITableViewCell {
posListViewController *control;
}
//UPDATED
@property (nonatomic, retain) id<CellHandlingDelegate> parentDelegate;
PosListViewCell.m
-(void)creat {
tapGestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(value1)];
tapGestureRecognizer1.numberOfTapsRequired = 1;
}
//Every cell has it own button when it is selected and click on the selected one it will be deleted.
//while it is selected the scroll should NOT be moving.
- (IBAction)btnEdit:(id)sender event:(id)event{
lblEdit.layer.borderColor = [UIColor redColor].CGColor;
lblEdit.layer.borderWidth = 1.0;
[lblLpl addGestureRecognizer:tapGestureRecognizer1];
control.tableV.scrollEnabled = NO;
NSLog(@"CLICKED");
}
-(void)value1 {
//Delete that value......
}
Upvotes: 1
Views: 291
Reputation: 1141
Storing the reference to your parent view controller in each cell is not the best way to implement this.
Why don't you implement a delegate which is implemented by your posListViewController instead. And from there. You can disable the scroll. Something like this.
@protocol CellHandlingDelegate <NSObject>
- (void) buttonWasSelected:(id)sender;
@end
Let your cell class have this:
@property (nonatomic, weak) id<CellHandlingDelegate> parentDelegate;
Then in posListViewController.h add
And in posListViewController.m add handler function.
- (void) buttonWasSelected:(id)sender{
// Disable scrolling of table
}
EDIT: Make the following changes:
`-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";
PosListViewCell *cell = (PosListViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PosListViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[PosListViewCell class]]){
cell = (PosListViewCell *)currentObject;
break;
}
}
}
cell.parentDelagate = self; <== ADD THIS !!!
NSDictionary *selectedContent = [data objectAtIndex:indexPath.row];
[cell setData:selectedContent];
return cell;
}
`
Upvotes: 2