分享

iOS自定义TableViewCell详解[两种步骤]

 叹落花 2014-12-15
 iOS自定义TableViewCell详解[两种方法]今天要跟大家分享的是两种自定义UITableViewCell方法。一、首先看看效

iOS自定义TableViewCell详解[两种方法]

今天要跟大家分享的是两种自定义UITableViewCell方法。

一、首先看看效果

1)第一种是通过nib文件加载的方式,在UITableView里面添加自定义的Cell。

iOS自定义TableViewCell详解[两种步骤]

2)第二种是代码里面自定义Cell的形式。

iOS自定义TableViewCell详解[两种步骤]

两种方式各有各的优点,根据不同的情况进行选择即可。


二、建立项目

1)建立SingleView项目,命名为CustomTableViewCell。

iOS自定义TableViewCell详解[两种步骤]

2)完成nib文件配置,View中只有一个UITableView控件,就是我们将要显示的表视图了。

设置style为Grouped,因为我们要把两种自定义Cell的方法用不同的分区显示。

iOS自定义TableViewCell详解[两种步骤]

iOS自定义TableViewCell详解[两种步骤]

别忘记配置表的代理,在此就不赘述了,有疑问的看看demo就懂了。


三、使用nib文件自定义Cell

1)建立自定义Cell的nib文件,在New File里面选择UserInterface中的Empty,取名为CustomCell。

iOS自定义TableViewCell详解[两种步骤]

2)接着,拉出6个label,摆出如下图所示(当然你可以自由发挥),注意中间的”|”也是一个label。为了能够在代码中找到Cell里面的控件,我们还需要设置label的Tag标记。(写死的label就不用了,例如“类型:”)。

iOS自定义TableViewCell详解[两种步骤]

设置Tag:

iOS自定义TableViewCell详解[两种步骤]

3)拖完控件之后,我们需要设置nib文件的控制器,修改File’s Owner的Class为YGViewController(对应自己程序中的表视图控制器),点击File’s Owner,然后在身份检查器中输入YGViewController。

iOS自定义TableViewCell详解[两种步骤]

iOS自定义TableViewCell详解[两种步骤]

注:一个控制器是可以加载多个nib文件的,这里我们的YGViewController就加载了YGViewController.nib和CustomCell.nib两个文件。

只要配置好输出口和操作的链接,我们就能有条不紊的对多个nib进行操作了。下面设定CustomCell.nib的输出口,取名teaCell。

-(UITableViewCell *)customCellWithOutXib:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath{    //定义标识符    static NSString *customCellIndentifier = @"CustomCellIndentifier";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIndentifier];        //定义新的cell    if(cell == nil){        //使用默认的UITableViewCell,但是不使用默认的image与text,改为添加自定义的控件        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:customCellIndentifier];        //姓名        CGRect nameRect = CGRectMake(88, 15, 70, 25);        UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameRect];        nameLabel.font = [UIFont boldSystemFontOfSize:nameFontSize];        nameLabel.tag = nameTag;//设置tag,以便后面的定位        nameLabel.textColor = [UIColor brownColor];        [cell.contentView addSubview:nameLabel];                //班级        CGRect classTipRect = CGRectMake(88, 40, 40, 14);        UILabel *classTipLabel = [[UILabel alloc]initWithFrame:classTipRect];        classTipLabel.text = @"班级:";        classTipLabel.font = [UIFont boldSystemFontOfSize:fontSize];        [cell.contentView addSubview:classTipLabel];                        CGRect classRect = CGRectMake(135, 40, 40, 14);        UILabel *classLabel = [[UILabel alloc]initWithFrame:classRect];        classLabel.tag = classTag;        classLabel.font = [UIFont boldSystemFontOfSize:fontSize];        [cell.contentView addSubview:classLabel];                //学号        CGRect stuNameTipRect = CGRectMake(88, 60, 40, 12);        UILabel *stuNameTipLabel = [[UILabel alloc]initWithFrame:stuNameTipRect];        stuNameTipLabel.text = @"学号:";        stuNameTipLabel.font = [UIFont boldSystemFontOfSize:fontSize];        [cell.contentView addSubview:stuNameTipLabel];                CGRect stuNameRect = CGRectMake(135, 60, 150, 14);        UILabel *stuNameLabel = [[UILabel alloc]initWithFrame:stuNameRect];        stuNameLabel.tag = stuNumberTag;        stuNameLabel.font = [UIFont boldSystemFontOfSize:fontSize];                [cell.contentView addSubview:stuNameLabel];                //图片        CGRect imageRect = CGRectMake(15, 15, 60, 60);        UIImageView *imageView = [[UIImageView alloc]initWithFrame:imageRect];        imageView.tag = imageTag;                //为图片添加边框        CALayer *layer = [imageView layer];        layer.cornerRadius = 8;//角的弧度        layer.borderColor = [[UIColor whiteColor]CGColor];        layer.borderWidth = 1;//边框宽度        layer.masksToBounds = YES;//图片填充边框        [cell.contentView addSubview:imageView];    }    //获得行数    NSUInteger row = [indexPath row];        //取得相应行数的数据(NSDictionary类型,包括姓名、班级、学号、图片名称)    NSDictionary *dic = [_stuArray objectAtIndex:row];        //设置图片    UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:imageTag];    imageV.image = [UIImage imageNamed:[dic objectForKey:@"image"]];        //设置姓名    UILabel *name = (UILabel *)[cell.contentView viewWithTag:nameTag];    name.text = [dic objectForKey:@"name"];        //设置班级    UILabel *class = (UILabel *)[cell.contentView viewWithTag:classTag];    class.text = [dic objectForKey:@"class"];        //设置学号    UILabel *stuNumber = (UILabel *)[cell.contentView viewWithTag:stuNumberTag];    stuNumber.text = [dic objectForKey:@"stuNumber"];        //设置右侧箭头    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}

1、在cell=nil里面摆好控件的位置,设置好相应的tag,然后在设置数据时,通过cell.contentViewviewWithTag:Tag 的形式定位到对应的Cell里面的tag。

2、通过NSUInteger row = [indexPath row]的到当前是哪一行,NSDictionary *dic = [_stuArrayobjectAtIndex:row];得到那一行的数据。


demo下载地址:http://download.csdn.net/detail/yang8456211/6409667

杨光(atany)原创,转载请注明博主与博文链接,未经博主允许,禁止任何商业用途。

博文地址:http://blog.csdn.net/yang8456211/article/details/12790487

博客地址:http://blog.csdn.net/yang8456211

—— by atany

本文遵循“署名-非商业用途-保持一致”创作公用协议








    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多