这是关于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......
}

最佳答案

在每个单元格中存储对父视图控制器的引用并不是实现此目的的最佳方法。

为什么不实现由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
}

编辑:
进行以下更改:
  • 在posListViewController.m中,将委托分配给self,如下所示。当您运行时,请告诉我是否正在调用委托函数。

  • `-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    静态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;
    }
    

    `
  • 委托成员变量应该弱不保留,否则可能会导致保留周期
  • 关于ios - 调用UITableView scrollEnabled问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25213049/

    10-10 14:21