1.UILabel的上对齐
UILabel的默认对齐方式只有左、中、右,而没有上对齐。若想实现这样的效果,最简单直接的方式就是在text后加一串" ",并且 之后最少有一个空格,否则会被UILabel忽略。如下
self.text = [myLabel.text stringByAppendingString:@"
"];
2.UITableView的分割线问题
默认情况下,cell之间的分割线会与左端有一定距离,如
比较简单的处理方式就是自定义分割线。添加分割线时推荐使用UIImageView添加背景色方法,不推荐在cell中用drawRect画线的方法,这样太耗资源
//先在tableview中取消cell之间的分割线
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//在cell中自定义分割线
UIImageView *lineImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, contentLab.bottom + 9, mScreenWidth, 0.5)];
lineImg.backgroundColor = [UIColor lightGrayColor];
[self.contentView addSubview:lineImg];
3.修改section之间的距离
如只设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
效果如下:
此时就需要设置section之间的距离,用
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 10;
}
设置后的效果如下,基本达到要求
4.若想设置如通讯录右侧索引,可以用
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"A",@"B",@"C",@"D",@"E"]; }
5.做通讯录分组时,需要以名字首字母拼音做组标题,思路就是对联系人的model进行设置,字典转模型
// .h文件 @interface ContactCellModel : NSObject @property(nonatomic,copy) NSString *contactImg; @property(nonatomic,copy) NSString *contactName; /** 首字母拼音 */ @property(nonatomic,copy) NSString *firstNamePinyin; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)contactWithDict:(NSDictionary *)dict; @end
//.m文件,需要#import "pinyin.h",这是一个老外写的将汉字转化为首字母拼音的转换文件 @implementation ContactCellModel -(instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { self.contactImg = [dict objectForKey:@"contactImg"]; self.contactName = [dict objectForKey:@"contactName"]; self.firstName = [[dict objectForKey:@"contactName"] substringToIndex:1]; self.firstNamePinyin = [NSString stringWithFormat:@"%c",pinyinFirstLetter([[dict objectForKey:@"contactName"] characterAtIndex:0])]; } return self; } +(instancetype)contactWithDict:(NSDictionary *)dict{ return [[self alloc]initWithDict:dict]; } @end
然后将首字母拼音相同的联系人放置到一组。
6.在解决iPhone一些横竖屏启动,界面错乱的问题,可以使用下面的宏定义
#define kScreenHeight MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); #define kScreenWidth MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
7.类似一些进度条慢慢延伸的动画,可以用UIProgressView。
//初始化 - (UIProgressView *)progressView{ if (_progressView == nil){ _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 39, 50, 1)]; _progressView.progress = 0.0; [_stateView addSubview:_progressView]; } return _progressView; } //进度条增长时的动画 - (void)uploadProgress{ self.progressView.progress += 0.013; if (self.progressView.progress > 0.8){ [self.timer invalidate]; self.timer = nil; } }
8.去除section的footer
当tableview设置为UITableViewStyleGrouped类型时,section标题背景默认是灰色的
但它是由两部分组成的,上一个section的footer和下一个section的header。通过以下代码就很明显
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc]init]; view.backgroundColor = [UIColor blueColor]; return view; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *view = [[UIView alloc]init]; view.backgroundColor = [UIColor redColor]; return view; }
效果为
拖到最后一个section,发现section的footer如下:
若想去掉最后一个section的footer,仅设置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ if (0 == section) { return 0; } return 15; }
这样是不行的,效果还是如上。应将其高度设为比1小很多,但不为0的值,才会有效果。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ if (0 == section) { return 0.00001f; } return 15; }
如下: