UITableViewCell有很多子视图或者只有一个视图



我是iOS编程新手,这就是为什么我正在寻找最有效的解决方案来解决我的问题。我想要实现的是在UITableViewCell中显示一个名称(一些文本),并在每个名称下一些填充的小矩形,里面有一个数字,类似于徽章。

我的第一个想法是创建一个代表徽章的UIView,在自定义UITableViewCell中,我将添加这些矩形作为subviews。第二个想法是只创建一个UIView来绘制所有的小矩形。

我的问题是,哪个是更好的解决方案,知道:

  • 单元格的数量将是最大的。20个,矩形总数不超过50个
  • 在单元格中显示的矩形数量不同
  • 我想重用单元格,所以我必须更新/重新绘制每一行的单元格内容
  • 我想避免"隐藏"子视图的单元格选择视图问题

当然,任何其他解决方案都是值得赞赏的。

提前感谢,hxx

我的建议是子类UITableViewCell,并使定制你需要在它。自定义视图下面可以有一个标签和矩形。

矩形可以是小的自定义按钮,带有背景图像(如果你有或给它一个背景色)和标题作为你的数字。但是,您必须根据表的宽度计算它们的宽度,以容纳矩形的最大数量。

你可以在xib中禁用表的选择,或者你可以像cell.selectionStyle = UITableViewCellSelectionStyleNone;那样编程,而不实现didSelectRowAtIndexPath

我遵循了子类化我的表的单元格的方法来自定义它们的外观和感觉,它工作得很好。我希望这对你有帮助。

在这里可以找到一个很好的开始子类化的教程

http://howtomakeiphoneapps.com/how-to-design-a-custom-uitableviewcell-from-scratch/1292/

为什么你不在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中创建单元格这里你可以定义你的自定义类型单元格,它也可以重用,每当你想要的时候,你可以像这样添加不同的东西到单元格。

static NSString *CellIdentifier = @"Cell";
UILabel *RequestSentTo;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    RequestSentTo = [[UILabel alloc] initWithFrame:CGRectMake(11, 2, 286, 40)];
    RequestSentTo.backgroundColor = [UIColor clearColor];
    RequestSentTo.tag = 200;
    RequestSentTo.numberOfLines = 3;
    RequestSentTo.font=[UIFont boldSystemFontOfSize:15.0];
    RequestSentTo.textColor = [UIColor blackColor];
    RequestSentTo.lineBreakMode=UILineBreakModeWordWrap;
    [cell.contentView addSubview:RequestSentTo];
} else {
    RequestSentTo=(UILabel*)[cell.contentView viewWithTag:200];
}
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Shift Request for "];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ by ",dateStr] attributes:nil]];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Dr. %@",notificationsObj.doctorName] attributes:purpleTextAttributes]];//purpl
RequestSentTo.attributedText=string;
RequestSentTo.lineBreakMode=UILineBreakModeWordWrap;
RequestSentTo.numberOfLines = 3;

只要你想,你可以添加你想要的东西与重用单元格。希望对大家有所帮助

我想到了两个方法。

  1. 您可以将组件作为UITableViewCell内的子视图(通过XIB或编程子类化UITableViewCell),并在UITableView中使用它。

  2. 您可以子类化UITableViewCell,并覆盖-(void)drawRect方法,并绘制您希望显示在cell上的所有组件

看看能不能帮上忙。你可以创建一个新的类扩展到UITableViewCell,这意味着重写UITableViewCell作为你自己的单元格命名为MyTestCell。在这个Cell中,你调用create你的属性,比如标签和视图,并将它们添加到你的新Cell中。比如把这个添加到MyTestCell.h

@property (nonatomic, retain) UILable *myLable1;
@property (nonatomic, retain) UIView *mySubview1;
MyTestCell.m
_myLable1 = .....
_mySubview = .....
[self addSubview: _myLbale1];
[self addSubview: _mySubview1];

And when use, u can work like this
static NSString *CellIdentifier = @"MyCell";
MyTableViewCell *cell = [tableview dequeReuseID:CellIdentifier]; 
if (cell == nil) {
  cell = [MyTableViewCell alloc] init.........
} 
//And you can sign your property here in your cell
cell.myLable1 = ....
cell.myView1 = .....
return cell;
}

如果添加到标签中的字符串不同,则创建标签。身高是不同的。您可以使用如下代码

 CGSize labelSize = [str sizeWithFont:[UIFont boldSystemFontOfSize:17.0f]
                   constrainedToSize:CGSizeMake(280, 100)
                   lineBreakMode:UILineBreakModeCharacterWrap];   //check your lableSize
 UILabel *patternLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 157, labelSize.width, labelSize.height)];   
 patternLabel.text = str;
 patternLabel.backgroundColor = [UIColor clearColor];
 patternLabel.font = [UIFont boldSystemFontOfSize:17.0f];
 patternLabel.numberOfLines = 0;// must have
 patternLabel.lineBreakMode = UILineBreakModeCharacterWrap;// must have

将此添加到单元格中,并使其动态调整标签和单元格的大小!你还需要动态设置tableviewrow的高度。(知道什么是动态调整大小吗?)看到这个:重写MyTableViewCell.m中的setMyLable1方法

 -(void)setMyLable1:(UILable*)aLable
 {
   //in here when never your sign your alabel to your cell (like this : cell.myLable1) this method will be call and u can get the size of your string and set this label's height
   //get string size  StringSzie
   [_myLable1 setFrame:CGRectMake(10,10,stringSize.width,stringSize.height)];
   //And resize your cell as well
   [self setFrame:CGRectMake(0,0,_myLable1.frame.size.width+20,_myLable1.frame.size.height+20)];
   //done!!!
 } 

你会得到一个自动调整单元格你也需要在tableView中动态重置行高度!!!!!

你需要什么叫做自定义单元格

这是一个很好的教程

为uitableview定制表格视图单元格

相关内容

最新更新