![](/img/trans.png)
[英]How get a custom UITableViewCell to appear in a UITableView in custom UIViewController with storyboard?
[英]How to configure UITableView inside custom UITableViewCell in a Storyboard?
我有一个layout
,其中将有2个带有自定义cells
UITableViews
。 第二个UITableView
必须在第一个UITableView
。
我的问题是:如何委托第二个UITableView
?
我可以将两者都委派给ViewController
吗? 在这种情况下,它将使用相同的方法,我必须找出现在管理哪个UITableView
。
还是我必须在第一个UITableView
自定义UITableViewCell
中委托它?
任何建议表示赞赏。
编辑:我不知道如何在这里实现解决方案,因为我有Storyboard
。 在当前的UIViewController
,将第一个UITableView
delegate
和dataSource
设置为视图控制器。
我的问题是我不知道如何设置第二个表视图的相同属性(将在UITableViewCell
)。 我不能将它们设置为UITableViewCell
(IB不允许这样做)。
IB的位置和位置如何设置?
更好的解决方案是将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实现可以遵循相同的过程,从而为您提供一个非常干净,分离和分离的代码库。 这样,您对两个表视图的需求本质上将更易于实现。
我的答案是
For identifying two table view data source and delegate method is,better to set tag for the table views.
在您的tableview委托方法中的编码下面进行设置。
if(tableView.tag==0)
{
}
else
{
}
另外,您可以通过为这些表视图分配不同的名称来更改此名称。
if(tableView==FirstTableView)
{
}
else
{
}
您只需检查每个委托方法的表条件
使用此代码注册自定义单元格。
-(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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.