这是我关于TableView
的第一个问题我有UITableViewController的视图和UITableViewCell的视图。我为UITableViewController创建了一个不同的CellView设计。
我的手机视图工作正常。我有一个小问题,一旦我点击btnEdit
它就不会阻止UITableViewController滚动。请问我该如何解决这个问题?
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......
}
答案 0 :(得分:2)
在每个单元格中存储对父视图控制器的引用并不是实现此目的的最佳方法。
为什么不实现由posListViewController实现的委托。从那里。您可以禁用滚动。这样的事情。
@protocol CellHandlingDelegate <NSObject>
- (void) buttonWasSelected:(id)sender;
@end
让你的细胞类有这个:
@property (nonatomic, weak) id<CellHandlingDelegate> parentDelegate;
然后在posListViewController.h中添加
在posListViewController.m中添加处理函数。
- (void) buttonWasSelected:(id)sender{
// Disable scrolling of table
}
修改强> 进行以下更改:
` - (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;
}
`