您的位置:首页 > 移动开发 > IOS开发

iOS中手工创建Cell和手工Tableview添加代理源

2015-06-05 16:19 471 查看
首先创建一个继承与UITableViewCell的类暂时称为TableViewCell

在点H文件中描述一下需要的属性

#import <UIKit/UIKit.h>

//这里我是需要三个属性,俩个Label一个image view
@interface TableViewCell :UITableViewCell

@property(nonatomic,strong)UILabel *Celllab1;

@property(nonatomic,strong)UILabel *Celllab2;

@property(nonatomic,strong)UIImageView *Cellimage;

@end
其次就是在点m文件增加了(可能用词不当,大家多多包涵)

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier
{
   self=[superinitWithStyle:style
reuseIdentifier:reuseIdentifier];
   if (self)
    {  //根据自己需要cell的大小和cell上控件的摆放来定义控件,高度根据自己设定的Cell的高度来定没这样能充满image view
       
_Cellimage=[[UIImageViewalloc]initWithFrame:CGRectMake(0,
0, 80,70)];
[self.contentViewaddSubview:_Cellimage];

        _Celllab1=[[UILabelalloc]initWithFrame:CGRectMake(82,
4, 200, 20)];

        [self.contentViewaddSubview:_Celllab1];

        _Celllab2=[[UILabelalloc]initWithFrame:CGRectMake(82,30
,self.contentView.bounds.size.width-112, 30)];

//contentView是cell自带的一个view,自己在XIB上拉一个也能看到他有一个自带的view,那个就是contentView,并不是描述的属性

//把自己需要的属性增加到contentView上面

        [self.contentViewaddSubview:_Celllab2];
    }

    return
self;

//注意,我上面并没有实例化Cell也没有给Cell一个标识符

}

然后转到
我添加tabview的view类里面
首先你要定义一个字符串用来当作是Cell的标识符

static NSString *ID=@"CELL";

在Tableview的代理源方法里重用cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{//在这个地方我才实例化cell

    TableViewCell *cell=[self.Tabview2dequeueReusableCellWithIdentifier:ID];
//假如cell为空的话就是没有
   if (cell==nil)
    {//就是创建一个cell,在这里实例化引用表示符

        cell=[[TableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
//这是cell上药添加的东西
        cell.Celllab1.text=lab1text[indexPath.row];
        cell.Celllab2.text=lab2text[indexPath.row];

        cell.Cellimage.image=[UIImageimageNamed:Cellimage[indexPath.row]];
    }

   return cell;

}
还有一个重要的事就是tableview是手工创建还是拉的,如果是拉的,在SB上连一下代理和协议
如果是手写的就要把代理源河协议加上

    

    tabview1.delegate=self;

    tabview1.dataSource=self;

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息