好久么写了,因为一直在赶项目。所以一直没空写博客,今天下午有点时间 我来说说我对控件的理解吧 也是对自己学习的一个总结。
iOS日常常用的控件:其实不是很多 也就UILabel,UITableview,UIImageView,UITextField,UIButton等等。。
首先拿到UI给的界面后进行分析,用什么控件去完成某个部分,
比如下个这个列表一样的东西 是用tableview比较好 还是用自定义的view
如果用tableview的话 用自定义的cell 还是系统自带的cell 等等的问题。
我刚刚进公司的时候 遇到这个 师兄们都说用view写,我看他一口气写五个view,controller上面都是view 看他的代码我头疼。
我就自己用tableview 重写吧,tableviewcell 用自带的还是自定义的那
cell系统自带的话有4种分别是:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};
至于4种样式分别是怎么样的 自己去试一下或者百度一下(学习不要那么懒对不对)
然后我发现我需要的样式和 UITableViewCellStyleValue1一样,所以我很偷懒的使用系统自带的了。
使用cell的时候不要忘记使用代理UITableViewDelegate和UITableViewDataSource至于代理是什么 为什么要用 留到下次说吧!
然后开始码代码了 #pragma mark 是一个很好的习惯
#pragma mark ——TableView——
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//定义个静态字符串为了防止与其他类的tableivew重复
static NSString *CellIdentifier =@"Cell";
//定义cell的复用性当处理大量数据时减少内存开销
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell setSeparatorInset:UIEdgeInsetsZero];//cell下划线长度
CGSize size = CGSizeMake(25*HEIGHT_SCALE, 25*HEIGHT_SCALE);
UIImage *image = [UIImage imageNamed:self.array[2*indexPath.row]];
cell.imageView.image = image;
cell.imageView.size = size;
//调整image的大小
UIGraphicsBeginImageContextWithOptions(size, NO,0.0);
CGRect imageRect=CGRectMake(0.0, 0.0, size.width, size.height);
[image drawInRect:imageRect];
cell.imageView.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.textLabel.text = self.array[2*indexPath.row+1];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 1;
}//多少组//
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}//多少行
#pragma mark ——懒加载——
-(UITableView *) iconTableView{
if(!_iconTableView)
{
_iconTableView= [[UITableView alloc]init];
_iconTableView.delegate = self;
_iconTableView.dataSource = self;
_iconTableView.tableFooterView = [[UIView alloc]init];
// _iconTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_iconTableView.scrollEnabled = NO;//不能滑动
self.iconTableView.rowHeight = 60*HEIGHT_SCALE;
}
return _iconTableView;
}
-(NSArray *) array{
if(!_array)
{
_array = [[NSArray alloc]init];
_array = [NSArray arrayWithObjects:@"Profile_collect_image",@"我的收藏",@"Profile_data_image",@"我的资料",@"Profile_class_image",@"课程记录",@"Profile_news_image",@"消息",@"Profile_set_image",@"设置",nil];
}
return _array;
}
至于布局我就不写了。