我在看《iphone开发》的时候,在表视图这一章遇到一个让我不解的地方,是关于向表视图单元添加子视图的一段代码。最后一段代码是这样的:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellTableIdentifier=@"CellTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell==nil) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
CGRect nameLabelRect = CGRectMake(0,5,70,15);
UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameLabelRect];
nameLabel.textAlignment = UITextAlignmentRight;
nameLabel.text=@"Name:";
nameLabel.font=[UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect colorLabelRect = CGRectMake(0,26,70,15);
UILabel *colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
colorLabel.textAlignment = UITextAlignmentRight;
colorLabel.text = @"Color:";
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:colorLabel];
[colorLabel release];
CGRect nameValueRect = CGRectMake(80,5,200,15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
nameValue.tag=kNameValueTag;
[cell.contentView addSubview:nameValue];
[nameValue release];
CGRect colorValueRect=CGRectMake(80, 25, 200, 15);
UILabel *colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
colorValue.tag = kColorValueTag;
[cell.contentView addSubview:colorValue];
[colorValue release];
}
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.computers objectAtIndex:row];
UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];
name.text = [rowData objectForKey:@"Name"];
UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag];
color.text = [rowData objectForKey:@"Color"];
return cell;
}我的疑问主要集中在最后一部分:最后新建了名为‘name’和‘color’的UILabel,并通过tag筛选了cell的contentView对应的子视图,并对子视图的Text字段赋值。但是,为什么返回cell就能让‘name’和‘color’两个UILabel绘制出来呢?这段代码里cell的值并没有被更改啊?只是进行了筛选。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellTableIdentifier=@"CellTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell==nil) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
CGRect nameLabelRect = CGRectMake(0,5,70,15);
UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameLabelRect];
nameLabel.textAlignment = UITextAlignmentRight;
nameLabel.text=@"Name:";
nameLabel.font=[UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect colorLabelRect = CGRectMake(0,26,70,15);
UILabel *colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
colorLabel.textAlignment = UITextAlignmentRight;
colorLabel.text = @"Color:";
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:colorLabel];
[colorLabel release];
CGRect nameValueRect = CGRectMake(80,5,200,15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
nameValue.tag=kNameValueTag;
[cell.contentView addSubview:nameValue];
[nameValue release];
CGRect colorValueRect=CGRectMake(80, 25, 200, 15);
UILabel *colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
colorValue.tag = kColorValueTag;
[cell.contentView addSubview:colorValue];
[colorValue release];
}
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.computers objectAtIndex:row];
UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];
name.text = [rowData objectForKey:@"Name"];
UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag];
color.text = [rowData objectForKey:@"Color"];
return cell;
}我的疑问主要集中在最后一部分:最后新建了名为‘name’和‘color’的UILabel,并通过tag筛选了cell的contentView对应的子视图,并对子视图的Text字段赋值。但是,为什么返回cell就能让‘name’和‘color’两个UILabel绘制出来呢?这段代码里cell的值并没有被更改啊?只是进行了筛选。
UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];
name这个Lable通过上面这行代码红色部分,用Tag筛选确定了应该使用nameValue这个Lable,所以前面有个(UILabel *)嘛,这就使得name这个Lable等同于nameValue这个Lable。
nameValue标签已经通过
CGRect nameValueRect = CGRectMake(80,5,200,15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];绘制了,只是没有设置它的text属性,只做了个Tag标记以便我们能找到它。
使用name.text = [rowData objectForKey:@"Name"];对name标签的Text属性赋值也就相当于对nameValue标签的Text属性赋值了,return cell后一切就顺理成章了。
nameValue.tag=kNameValueTag;一个是通过tag查找,很容易就对应上了
UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];之后的修改就顺理成章了
然后不是add到contentview上了吗?