iphone开发基础_向表视图添加子视图_第八章_P157
H:
View Code
#define kNameValueTag 1
#define kColorValueTag 2
#define kColorValueTag 2
M:
View Code
//显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建一个字符串,作为标识
static NSString *CellTableIdentifier=@"CellTableIdentifier";
//dequeueReusableCellWithIdentifier的作用是给cell打个标记,tableview划出屏幕的时候,将cell放入队列,当下面用到相同的cell,就不需要创建,直接取出使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
//如果对象不存在的操作
if (cell==nil) {
//创建默认样式,并且使用标识
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
//创建一个Label
//定义一个位置,x、y、width、height
CGRect nameLabelRect = CGRectMake(0,5,70,15);
//创建UILable,并定义好位置
UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameLabelRect];
//定义位置
nameLabel.textAlignment = UITextAlignmentRight;
//设置显示文字
nameLabel.text=@"Name:";
//字体大小
nameLabel.font=[UIFont boldSystemFontOfSize:12];
//添加子视图显示
[cell.contentView addSubview:nameLabel];
//释放
[nameLabel release];
//创建第二个Label
//具体内容,同上,此处表示Color
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];
//创建第三个Lable,根据Name
CGRect nameValueRect = CGRectMake(80,5,200,15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
//设置标签
nameValue.tag=kNameValueTag;
//添加子视图
[cell.contentView addSubview:nameValue];
[nameValue release];
//同上,此处根据Color
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];
//获取Tag等于kNameValueTag的Label
UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];
//f赋值
name.text = [rowData objectForKey:@"Name"];
//根据Color
UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag];
//赋值
color.text = [rowData objectForKey:@"Color"];
//返回数据
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建一个字符串,作为标识
static NSString *CellTableIdentifier=@"CellTableIdentifier";
//dequeueReusableCellWithIdentifier的作用是给cell打个标记,tableview划出屏幕的时候,将cell放入队列,当下面用到相同的cell,就不需要创建,直接取出使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
//如果对象不存在的操作
if (cell==nil) {
//创建默认样式,并且使用标识
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
//创建一个Label
//定义一个位置,x、y、width、height
CGRect nameLabelRect = CGRectMake(0,5,70,15);
//创建UILable,并定义好位置
UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameLabelRect];
//定义位置
nameLabel.textAlignment = UITextAlignmentRight;
//设置显示文字
nameLabel.text=@"Name:";
//字体大小
nameLabel.font=[UIFont boldSystemFontOfSize:12];
//添加子视图显示
[cell.contentView addSubview:nameLabel];
//释放
[nameLabel release];
//创建第二个Label
//具体内容,同上,此处表示Color
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];
//创建第三个Lable,根据Name
CGRect nameValueRect = CGRectMake(80,5,200,15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
//设置标签
nameValue.tag=kNameValueTag;
//添加子视图
[cell.contentView addSubview:nameValue];
[nameValue release];
//同上,此处根据Color
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];
//获取Tag等于kNameValueTag的Label
UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];
//f赋值
name.text = [rowData objectForKey:@"Name"];
//根据Color
UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag];
//赋值
color.text = [rowData objectForKey:@"Color"];
//返回数据
return cell;
}
理解直接复制的csdn的coolcloud88见解:
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后一切就顺理成章了。