簡體   English   中英

如何將subView添加到tableViewCell?

[英]How to add a subView to tableViewCell?

這是我在情節提要中的自定義單元格。請注意,我為原型單元分配了標簽123

這是我對tableView的委托和數據源方法的實現

但是,我無法成功將子視圖添加到tableView Cell中。 請幫幫我! 我的項目的下載鏈接可在此處獲得。

在您的“ cellForRowAtIndexPath”中進行操作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
myView.backgroundColor = [UIColor redColor];
[cell addSubview:myView];
return cell;
}

您應該將其添加為單元的子視圖。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:"MyCell" forIndexPath:indexPath];

  if (nil == cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:"MyCell"];
  }
  UIView *aView = [UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  [cell addSubview: aView];
  aView.backgroundColor = [UIColor redColor];
  return cell;
}

這是你的錯誤,紅線。

在此處輸入圖片說明

在“下面的鏈接”中找到更新的工作演示,

樣本演示

在此處輸入圖片說明 在您的項目中,您尚未將ViewController設置為初始視圖控制器。

除此之外,您的代碼運行正常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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