下面給出得是一個label添加到cell中的例子
將具體的視圖實例,添加到cell的contentView中
注意:如何在父視圖中標識某個子視圖?
step1:在添加子視圖時,指定tag值(大於0)
step2:為了找出這個子視圖,通過調用父視圖的 viewWithTag 方法,就可以找到指定的視圖
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1" ];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell1"];
}
//把索引給系統 forIndexPath:indexPath為注冊准備的,如果要自己創建的話,就把這個去掉
// cell.detailTextLabel.text=@"detail.. ";
// cell.imageView.image=[UIImage imageNamed:@"icon1.png"];
//cell.textLabel.text=@"helo";
//先按照tag值試着去Cell中找,是否有標簽
UILabel *label=(UILabel*)[cell.contentView viewWithTag:1];
if (label==nil) {//如果沒找到再新建
label=[[UILabel alloc]init];
//為label設置tag值
label.tag=1;
label.textColor=[UIColor redColor];
label.font=[UIFont italicSystemFontOfSize:40];
label.frame=CGRectMake(0, 0, cell.bounds.size.width, 42);
label.textAlignment=NSTextAlignmentCenter;
[cell.contentView addSubview:label];
}
label.text=[NSString stringWithFormat:@" %ld", indexPath.row];
return cell;
}