簡體   English   中英

如何在情節提要中的自定義UITableViewCell中配置UITableView?

[英]How to configure UITableView inside custom UITableViewCell in a Storyboard?

我有一個layout ,其中將有2個帶有自定義cells UITableViews 第二個UITableView必須在第一個UITableView

我的問題是:如何委托第二個UITableView

我可以將兩者都委派給ViewController嗎? 在這種情況下,它將使用相同的方法,我必須找出現在管理哪個UITableView

還是我必須在第一個UITableView自定義UITableViewCell中委托它?

任何建議表示贊賞。

編輯:我不知道如何在這里實現解決方案,因為我有Storyboard 在當前的UIViewController ,將第一個UITableView delegatedataSource設置為視圖控制器。

我的問題是我不知道如何設置第二個表視圖的相同屬性(將在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM