【问题标题】:Delegates and datasource not working for UIPopoverListView in iOS?代表和数据源不适用于 iOS 中的 UIPopoverListView?
【发布时间】:2014-03-01 23:07:48
【问题描述】:

我从 github 下载了 UIPopOverListView,并粘贴到我的工作区,然后当单击按钮时,会出现 popoverlistview,但调用了委托和数据源方法但无法正常工作,
当我的按钮被点击时

-(void) effectsButtonClicked
{
effectsPopView = [[UIPopoverListView alloc] initWithFrame:CGRectMake(10,150,300,200 )];
effectsPopView.delegate=self;
effectsPopView.datasource=self;
effectsPopView.listView.scrollEnabled = FALSE;
[effectsPopView setTitle:@"Effects"];
[effectsPopView show];
}

那么我的数据源和委托是

#pragma mark - UIPopOverListView DataSource

- (NSInteger)popoverListView:(UIPopoverListView *)popoverListView
   numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)popoverListView:(UIPopoverListView *)popoverListView
                cellForIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell * effectsCell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:identifier];

effectsArray=[NSArray arrayWithObjects:@"Black and White",@"Sepia",@"Hue",@"Snow",@"Normal", nil];
effectsCell.textLabel.text=[effectsArray objectAtIndex:indexPath.row];
effectsCell.textLabel.textColor=[UIColor redColor];
NSLog(@"%@",effectsArray);
return effectsCell;
}

#pragma mark- UIPopoverList Delegates

- (void)popoverListView:(UIPopoverListView *)popoverListView
 didSelectIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"ROw selected");
}  

屏幕截图是

谁能解释一下,为什么数据源和委托不工作

【问题讨论】:

    标签: ios objective-c delegates datasource


    【解决方案1】:

    有两种连接数据源的方法 从 (1)从文件所有者添加 (2) 从界面

    【讨论】:

      【解决方案2】:

      似乎UIPopoverListView 在内部实现了UITableView。在所有表视图数据源方法中,它正在调用您正在实现的公开的datasource 方法。

      我认为的问题是delegate 和datasource 的分配发生在UITableView 数据源方法被调用之后。在设置UIPopoverListView 数据源后,没有机制可以调用它们。作为一种解决方法,您可以实现一个 reload 方法,该方法只会在内部表视图实例上调用 reloadData。

      -(void) reload {
          if(_listView) {
              [_listView reloadData];
          }
      }
      

      在.h文件中声明这个方法并在你的代码中调用它,

       -(void) effectsButtonClicked
       {
            effectsPopView = [[UIPopoverListView alloc] initWithFrame:CGRectMake(10,150,300,200 )];
            effectsPopView.delegate=self;
            effectsPopView.datasource=self;
            effectsPopView.listView.scrollEnabled = FALSE;
            [effectsPopView setTitle:@"Effects"];
            //Call here before presenting.
            [effectsPopView reload];
            [effectsPopView show];
      

      }

      这可能不是理想的解决方案,只是一种解决方法。您应该在项目的 github 页面上提交错误,以便原作者可以提供修复。

      希望有帮助!

      【讨论】:

        【解决方案3】:

        你必须在你的 UIPOpoverListView 类中实现类似默认初始化的方法 要么 您需要在 popoverltstView Show 方法中重新加载表格 -(无效)显示 { //这里重新加载表格

        }

        【讨论】:

          猜你喜欢
          • 2015-07-11
          • 2016-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-07
          • 2020-04-27
          • 2013-03-01
          • 2017-05-15
          相关资源
          最近更新 更多