如何在情节提要中的自定义 UITableViewCell 中配置 UITableView?
How to configure UITableView inside custom UITableViewCell in a Storyboard?
我有一个 layout
,其中我将有 2 个 UITableViews
自定义 cells
。第二个 UITableView
必须在第一个里面。
我的问题是:如何委托第二个UITableView
?
我可以将两者委托给我的 ViewController
吗?在那种情况下,它将使用相同的方法,我必须找出现在管理的 UITableView
。
或者我必须在第一个 UITableView
的自定义 UITableViewCell
中委派它?
如有任何建议,我们将不胜感激。
编辑:我不知道如何在这里实施解决方案,因为我有 Storyboard
。在我当前的 UIViewController
中,我将第一个 UITableView
的 delegate
和 dataSource
设置为我的视图控制器。
我的问题是我不知道如何设置第二个 Table 视图(将在 UITableViewCell
内)的相同属性。我不能将它们设置为 UITableViewCell
(IB 不允许这样做)。
然后在 IB 中在哪里以及如何设置?
您只需检查每个委托方法的table条件
使用此代码注册自定义单元格。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.yourFirstTable)
{
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellModifier"];
// your code
}
else
{
// second table cell code
}
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.yourFirstTable)
{
// first tableView number of row return
}
else
{
// second table number of row return
}
}
并在 TableView
中创建原型单元格
并像这样设置 CellReusableId
我的答案是
For identifying two table view data source and delegate method is,better to set tag for the table views.
在您的 tableview delegates 方法中设置下面的代码。
if(tableView.tag==0)
{
}
else
{
}
您还可以通过为这些 table 视图指定不同的名称来改变这一点。
if(tableView==FirstTableView)
{
}
else
{
}
一个更好的解决方案是将 DataSource 和 Delegate 实现从视图控制器中抽象出来,以便可以根据需要对每个表视图进行个性化(请注意,代码取自 objc.io文章Lighter View Controllers.
例如
@implementation ArrayDataSource
- (id)itemAtIndexPath:(NSIndexPath*)indexPath {
return items[(NSUInteger)indexPath.row];
}
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
return items.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
configureCellBlock(cell,item);
return cell;
}
@end
那么你可以如下使用它:
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
cell.label.text = photo.name;
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;
UITableViewDelegate 实现可以遵循相同的过程,为您提供一个非常干净、分离和解耦的代码库。您对两个表视图的要求本质上更容易实现。
我有一个 layout
,其中我将有 2 个 UITableViews
自定义 cells
。第二个 UITableView
必须在第一个里面。
我的问题是:如何委托第二个UITableView
?
我可以将两者委托给我的 ViewController
吗?在那种情况下,它将使用相同的方法,我必须找出现在管理的 UITableView
。
或者我必须在第一个 UITableView
的自定义 UITableViewCell
中委派它?
如有任何建议,我们将不胜感激。
编辑:我不知道如何在这里实施解决方案,因为我有 Storyboard
。在我当前的 UIViewController
中,我将第一个 UITableView
的 delegate
和 dataSource
设置为我的视图控制器。
我的问题是我不知道如何设置第二个 Table 视图(将在 UITableViewCell
内)的相同属性。我不能将它们设置为 UITableViewCell
(IB 不允许这样做)。
然后在 IB 中在哪里以及如何设置?
您只需检查每个委托方法的table条件
使用此代码注册自定义单元格。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.yourFirstTable)
{
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellModifier"];
// your code
}
else
{
// second table cell code
}
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.yourFirstTable)
{
// first tableView number of row return
}
else
{
// second table number of row return
}
}
并在 TableView
并像这样设置 CellReusableId
我的答案是
For identifying two table view data source and delegate method is,better to set tag for the table views.
在您的 tableview delegates 方法中设置下面的代码。
if(tableView.tag==0)
{
}
else
{
}
您还可以通过为这些 table 视图指定不同的名称来改变这一点。
if(tableView==FirstTableView)
{
}
else
{
}
一个更好的解决方案是将 DataSource 和 Delegate 实现从视图控制器中抽象出来,以便可以根据需要对每个表视图进行个性化(请注意,代码取自 objc.io文章Lighter View Controllers.
例如
@implementation ArrayDataSource
- (id)itemAtIndexPath:(NSIndexPath*)indexPath {
return items[(NSUInteger)indexPath.row];
}
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
return items.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
configureCellBlock(cell,item);
return cell;
}
@end
那么你可以如下使用它:
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
cell.label.text = photo.name;
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;
UITableViewDelegate 实现可以遵循相同的过程,为您提供一个非常干净、分离和解耦的代码库。您对两个表视图的要求本质上更容易实现。