0

我正在尝试创建一个UITableView使用它自己的自定义扩展的类DataSource

该类称为SHCTableView.

首先,我创建了一个名为SHCTableViewDataSource.hextends的原型类NSObject

现在我将属性添加到SHCTableView.h

// the object that acts as the data source for this table
@property (nonatomic, assign) id<SHCTableViewDataSource> dataSource;

我收到警告

Property type 'id<SHCTableViewDataSource>' is incompatible with type 'id<UITableViewDataSource>' inherited from 'UITableView'

并且在SHCTableView.m

const float SHC_ROW_HEIGHT = 50.0f;

-(void)refreshView {
  // set the scrollview height
  _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, [_dataSource numberOfRows] * SHC_ROW_HEIGHT);

  // add the cells
  for (int row=0; row < [_dataSource numberOfRows]; row++) {

    // obtain a cell
    UIView* cell = [_dataSource cellForRow:row];

    // set its location
    float topEdgeForRow = row * SHC_ROW_HEIGHT;
    CGRect frame = CGRectMake(0, topEdgeForRow, _scrollView.frame.size.width, SHC_ROW_HEIGHT);
    cell.frame = frame;

    // add to the view
    [_scrollView addSubview:cell];

  }

}

#pragma mark - property setters
-(void)setDataSource:(id<SHCTableViewDataSource>)dataSource {

  _dataSource = dataSource;
  [self refreshView];

}

所有实例_dataSource都给我一个错误:

Instance variable '_dataSource' is private

还有一堆其他错误,但它们似乎都是由于_dataSource私有的事实引起的,这可能与上面的警告有关。

任何帮助将不胜感激。

4

1 回答 1

0

UITableView 已经声明了 dataSource,所以声明为同名并不是一个好主意。只需声明

@property (nonatomic, assign) id<SHCTableViewDataSource> customDataSource;

然后

-(void)setDataSource:(id<SHCTableViewDataSource>)dataSource {
[super setDataSource:_dataSourceInterceptor]
  _customDataSource = dataSource;

 [self refreshView];

}

如果你必须这样做,那么你可以这样做..

创建中间对象为dataSourceInterceptor,设置超类的原始dataSource,实现dataSource函数。不要将 self 设置为 dataSource 这不是一个好主意。

于 2013-03-12T23:07:30.510 回答