gpt4 book ai didi

ios - 调用 UITableView scrollEnabled 问题

转载 作者:行者123 更新时间:2023-11-29 10:39:28 25 4
gpt4 key购买 nike

这是我关于 TableView 的第一个问题 我有一个 UITableViewController View 和一个 UITableViewCell View 。我为 UITableViewController 创建了一个不同的 CellView 设计。

我的单元格 View 工作正常。我有一个小问题,一旦我点击 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......
}

最佳答案

在每个单元格中存储对父 View Controller 的引用并不是实现此目的的最佳方式。

为什么不实现一个由 posListViewController 实现的委托(delegate)呢?从那里开始。您可以禁用滚动。像这样。

@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
}

编辑:进行以下更改:

  1. 在 posListViewController.m 中,将委托(delegate)分配给 self,如下所示。当你运行时请告诉我是否正在调用委托(delegate)函数。

`-(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;
}

`

  1. delegate成员变量应该是weak不retain否则会造成retain cycle

关于ios - 调用 UITableView scrollEnabled 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com