繁体   English   中英

没有StoryCell的customCell类的CustomCell

[英]CustomCell without customCell class with storyboard

我会创建一个自定义单元而不创建一个自定义单元类。 我知道这是可能的。 我在情节提要中创建了一个带有带有标签的原型单元的tableviewcontroller。 在服装中,我将单元格名称设置为“ mycell”。 我使用的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  static NSString *CellIdentifier = @"mycell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) 
   { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
   UILabel *title = (UILabel*) [cell viewWithTag:1];
   title.text = @"Hi";
   return cell;
}

但是当我的应用程序运行时,我只会看到一个空表,没有带有标签的单元格。 在tableView方法中,我使用了:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return 5; }

谢谢。

如果要以编程方式添加UILabel,则需要将其分配为alloc init,应为其指定一个框架并将其作为子视图添加到单元格中。

UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = @"Hi";
return cell;

应该

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 50, 20)];
title.text = @"Hi";
[cell addSubview:title];
return cell;

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM