IOS tableview 小总结 (2)
最近比较忙没时间总结,好容易项目一期没事了,下面就八一八吧。上一篇给的数据都是死的,这样不好,因为我们不确定服务器传过来是什么东西,所以总体思想就是,写成活的,写成一个动态数组,把值传进去,在把动态数组的数据传给表这样就可以谁来显示谁,挺好的的。
1,首先我们可以专门定义一个tableviewcell的类,这这里面我们可以绘制自己想要的cell,self = cell.操作起来比较方便。
2,我们在调用这个cell的父类也就是super中可以这样子写。
@interface MybibiViewController ()
<PersonInfoCellDelegate,UITableViewDataSource,UITableViewDelegate>
{
}
这里的PersonInfoCellDelegate,就是我们自己写的cell代理,当然相对应的在Cell类中也得明确表示有这个东东
@protocol PersonInfoCellDelegate <NSObject>
- (void)cellOnClick:(NSIndexPath *)index withBt:(UIButton *)bt;
- (void)normalCellShareBtOnClick:(NSIndexPath *)index;
@end
<pre name="code" class="objc">@interface PersonInfoCell : UITableViewCell
{
id<PersonInfoCellDelegate>delegate;
}
<pre name="code" class="objc">@property (nonatomic,retain)id<PersonInfoCellDelegate>delegate;
里面我写了两个方法,注意,里面有
(NSIndexPath *)index
很明显这个只有在父类们也就是定义tableview才能用,index里面有两个一个是section,一个是row,这个应该都知道,对应了tableview中的各种cell,有个这个妈妈再也不用担心我找特定的cell了,这两个方法也是在tableview中的那个类才能使用,name若何才能将这个方法传过去呢?这应该写在cell类里
- (void)share:(UIButton *)bt {
[self.delegate normalCellShareBtOnClick:myindex];
}
有代理就是这么任性,这里就是让父类调用这个方法。
3.最重要的是传入数据,我们可以建立一个动态数组
//对CELL进行描述
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//tableview.backgroundColor = RGBACOLOR(238, 238, 238, 1);
static NSString *cellStr = @"TableSampleIdentifiers";
PersonInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellStr]; //将cell定义为我们自定义的
int row = indexPath.row;
NSDictionary *dic = [dataList objectAtIndex:row];//这句话的意思就是根据table的row来,把已经写好在里面的datalist的数据取出来,我们要哪个就取哪个,事先声明datalist原来就有数据
if (cell == nil) {
cell = [[PersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
}
[cell setData:dic indexPath:indexPath];//这个方法是给cell赋值,因为我们已经拿到了数据,拿到数据就给它赋值一下就好了
return cell;//返回cell
}
本文来自博客园,作者:赫凯,转载请注明原文链接:https://www.cnblogs.com/heKaiii/p/15491363.html