UICollectionView
collectionView和tableView最大的不同之处就是:collectionView能自动的根据屏幕宽度显示横列竖!
/// 别忘了写 代理 <UITableViewDelegate,UITableViewDataSource>
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416) style:UITableViewStylePlain];//Grouped 分组样式
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView release];
//属性
//分割线颜色
_tableView.separatorColor = [UIColor redColor];
//分割线类型:三种
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//行高
_tableView.rowHeight = 50;
_tableView.sectionHeaderHeight = 20;
_tableView.sectionFooterHeight = 20;
//背景颜色
_tableView.backgroundColor = [UIColor yellowColor];
///背景图片
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
imageView.image = [UIImage imageNamed:@"10_0.jpg"];
//背景图片
_tableView.backgroundView = imageView;
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 20 + indexPath.row * 10;
}
//组标题header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return section * 40 + 40;
}
//footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
//自定义组视图
//- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// button.frame = CGRectMake(0, 0, 320, 40);
// return button;
//}
//多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//组头标题
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section==0)
{
return @"第一组";
}
if(section==1)
{
return @"第二组";
}
return nil;
}
//组 结尾标题
- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"end";
}
//必写
//多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"多少行");
return 20;
}
//必写
//创建tableViewCell,每一行要显示的内容
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* tableIder = @"ID";
//从叫做@“ID”的队列里面去取出cell
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:tableIder];
//判断队列里面是否存在cell
if (cell == nil) {
//如果不存在,那么我们创建一个cell
//两个参数,第一个参数是cell的类型,第二个是当cell移出屏幕时,要保存到的队列名称
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIder] autorelease];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 20)];
label.tag = 10;
label.font = [UIFont systemFontOfSize:15.0];
label.backgroundColor = [UIColor clearColor];
[cell addSubview:label];
[label release];
}
UILabel* label = (UILabel*)[cell viewWithTag:10];
label.text = [NSString stringWithFormat:@"%d",indexPath.row];
//indexPath.section
//cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
cell 的 颜色交错
1 cell的颜色不能单纯的在cell的方法里设置background ,可以通过创建UIView并设置backgrounColor后 设置cell的backgroundView = UIView来实现
2 可以自WillDisplayCell 里面直接设置cell.backgroundColor
瀑布流:展示层次不齐的图片
////这个是核心 三个或者N个tableView联动 设置
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSArray *array = [[NSArray alloc] initWithObjects:leftTableView,middleTableView,rightTableView, nil];
for (UIScrollView *s in array) {
if (s != scrollView) {
s.contentOffset = scrollView.contentOffset ;
}
}
}
cell 和contentView 的关系
contentView 是放在cell上面的 除了编辑状态 显示效果一样 。
cell =
cell.contentView =
尽量把东西放在contentView 上
cell 很乱的情况 :
方法1 :
// for (UIView *v in [cell.contentView subviews]) {
// [v removeFromSuperview];
// }
方法2:
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
重叠:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}else{
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; //删除并进行重新分配
}
}
动画效果 :
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15];
myView.frame = CGRectMake(140, 360, 80, 60) ; ///控制myView的位置
[UIView commitAnimations];
//可以设置NSTimer 设置显示时间
cell 点击效果
在cellForRowAtIndexPath:方法中写上
cell.selectionStyle = UITableViewCellSelectionStyleNone;
点击时 cell上的控件显示颜色消失
在cellForRowAtIndexPath:方法中写上
UIView *view_bg = [[UIView alloc] initWithFrame:cell.frame];
view_bg.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = view_bg;
////背景颜色闪一下消失
在didSelectRowAtIndexPath:方法中写上
[tableView deselectRowAtIndexPath:indexPath animated:NO];
网友评论