- 浏览: 420793 次
- 性别:
- 来自: 深圳/湛江
文章分类
最新评论
-
wangyudong:
很多API doc生成工具生成doc需要重度依赖代码里加注解的 ...
[ios]利用xcode自动生成项目文档 -
tiger1819:
有源码么,想学习
[android]仿360状态,类流量监控桌面浮动显示 -
119568242:
借我一双翅膀 写道大哥,求指教啊?
IOS 开发,调用打电话,发短信,打开网址 -
借我一双翅膀:
大哥,求指教啊
IOS 开发,调用打电话,发短信,打开网址 -
li247276297:
楼主 是不是要加个权限?
[android]仿360状态,类流量监控桌面浮动显示
[NSString*对象 sizeWithFont:constrainedToSize:]
这个方法用于获取 以传入模式下的 字符串像素大小。
这玩意今天坑了我很久。表示 各种陷阱。
如果你是从android转过来的 请看看下面。
首先 如果使用ios6的autolayout你会在最后发现你被坑了。
然后ios 的tablecell不会随着你的内容变化的缩小放大。
lableView也不会。
更蛋疼的是lableView不会换行--》也就是说你需要自己处理换行。今天用了个递归去+“/n”
自己换行就算了- -,还需要自己设置行数。否则显示不出来的亲。
【改:如果你把行数默认成0的话 他会自适应行数 也就是说 你内容有多少行他就多少行 但是frame还是要自己设置】
计算行数
【计算移动距离不需要通过行数。】
str为输入内容 move 为算出 当前frame相对于单行是移动了多少。[用于给其他受影响的view修改orgain.x]
CGSize size=[str sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(210.0f,1000.0f ) lineBreakMode:NSLineBreakByWordWrapping];
//将str字符串以sizeWithFont 的字体模式 单词切割模式 传入指定的大小的size[宽度超过210就换行]内 所需要的实际高度和宽度。
//【传入的size 为限制值,高度和宽度最大只能为传入宽度大小】
CGSize size2=[@"1" sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(210.0f,1000.0f ) lineBreakMode:NSLineBreakByWordWrapping];
int move=size.height-size2.height
总结一下:
1.你需要知道你的内容的行数。
2.你需要对你内容 进行换行处理并且设置行数。
【在stroyboard里面设置lable行数为0,既可自动换行】
3.你需要根据你内容的行数修改你的布局。包括自己的frame.size.height,被影响的frame.origen.y[计算出 位移(当前size-@""的size),然后给+被影响的]。
4.你还要修改cell的高度。
5.确定不要使用autoLayout。
下面是demo 这个demo里面很多老东西 可以别看- -
// // tableViewVC.m // swearWorldDemo // // Created by liu poolo on 12-10-17. // Copyright (c) 2012年 liu poolo. All rights reserved. // #import "tableViewVC.h" #import "MyXMLParser.h" #import "SwearVO.h" #import "CountryVO.h" #import <Social/Social.h> #import <QuartzCore/QuartzCore.h> #import "ButtonCell.h" @interface tableViewVC () @property NSMutableArray *array; @property NSString *pathSwear; @property (nonatomic) NSString *pathCountry; @property NSInteger currentSection; @property NSString *text; @property UIImage *image; @property NSArray *fireImages; @end @implementation tableViewVC @synthesize array=_array; @synthesize pathCountry=_pathCountry; @synthesize pathSwear=_pathSwear; @synthesize swearDy=_swearDy; @synthesize countryDy=_countryDy; @synthesize text=_text; @synthesize image=_image; @synthesize fireImages; - (void)setSwearDy:(NSArray *)swearDy{ if(_swearDy!=swearDy){ _swearDy=swearDy; [self.tableView reloadData]; NSLog(@"setSwearDy"); } } - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // self.pathSwear=[[NSBundle mainBundle] pathForResource:@"swearList" ofType:@"plist"]; // self.swearDy=[NSArray arrayWithContentsOfFile:self.pathSwear]; // self.pathCountry=[[NSBundle mainBundle] pathForResource:@"countryList" ofType:@"plist"]; // self.countryDy=[NSArray arrayWithContentsOfFile:self.pathCountry]; self.currentSection=-1; NSLog(@"viewDidLoad"); //设置table的一些参数 self.tableView.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.jpg"]]; self.tableView.sectionIndexColor=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1]; self.tableView.sectionIndexTrackingBackgroundColor=[UIColor colorWithRed:0.50196081399917603 green:0.25098040699958801 blue:0.0 alpha:0.6]; //加载fireImage; self.fireImages=@[[UIImage imageNamed:@"fire1.png"],[UIImage imageNamed:@"fire2.png"],[UIImage imageNamed:@"fire3.png"],[UIImage imageNamed:@"fire4.png"],[UIImage imageNamed:@"fire5.png"],[UIImage imageNamed:@"fire6.png"]]; } -(void)viewWillAppear:(BOOL)animated{ NSLog(@"viewWillAppear"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.countryDy count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger countryID=[self getCountryIdBySession:section]; return [[self getArrayByCountryId:countryID] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"swearInfo"; ButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if(cell==nil){ cell=[[ButtonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if(self.currentSection!=indexPath.section){ //进入新的section了 NSInteger countryID=[self getCountryIdBySession:indexPath.section]; self.array=[[self getArrayByCountryId:countryID] mutableCopy]; } cell= [self CellSetedByRow:indexPath.row withArray:self.array andCell:cell]; // 修改 右边 导航栏背景色 残疾法 // for(UIView *view in [tableView subviews]) // { // // if([[[view class] description] isEqualToString:@"UITableViewIndex"]) // { // //// //// if(indexPath.row%2){ //// [view setBackgroundColor:[UIColor yellowColor]]; //// }else{ //// [view setBackgroundColor:[UIColor blueColor]]; //// } //// ////// [view setFont:[UIFont systemFontOfSize:14]]; // } // } return cell ; } - (NSArray *)getArrayByCountryId:(NSInteger) countryid{ //根据国家ID获取对应粗口数组 NSMutableArray * tempArray=[[NSMutableArray alloc]init]; for(CountryVO *a in self.swearDy){ if([a.countryId integerValue]==countryid){ [tempArray addObject:a]; } } return tempArray; } - (NSInteger)getCountryIdBySession:(NSInteger) section{ //通过session获得国家id NSInteger result=0; CountryVO * tempD=(CountryVO *)[self.countryDy objectAtIndex:section]; result =[tempD.countryId integerValue]; return result; } - (NSString *)CountryNameBySession:(NSInteger) section{ //通过session获得国家名 NSString *result=0; CountryVO *tempD=(CountryVO *)[self.countryDy objectAtIndex:section]; result = tempD.countryName; return result; } // //-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // // //} #define BASEHEIGHT 76 //cell初始状态高度 #define PERHEIGHT 20 //每次行增加距离 #define DEFAULT 27 //初始name 高度 #define rowSize 22 //每行字节数 #define DEFAULTDSCRIBE 24 //出状态DSCRIBE 高度 - (ButtonCell *)CellSetedByRow:(NSInteger)row withArray:(NSArray *)a andCell:(ButtonCell *)cell{ //根据行号,对应的数组,和CELL设置CELL。 SwearVO * swearVO= (SwearVO *)[a objectAtIndex:row]; // UILabel * labCountry=(UILabel *)[cell viewWithTag:-1]; // UILabel * labName=(UILabel *)[cell viewWithTag:-2]; // UILabel * labLevel=(UILabel *)[cell viewWithTag:-3]; //// UILabel * labImagePath=(UILabel *)[cell viewWithTag:-4]; // UILabel * labdescribe=(UILabel *)[cell viewWithTag:-5]; // UIButton *bt=(UIButton *)[cell viewWithTag:-6]; // ((UILabel *)[cell viewWithTag:10]).text=@"这里是Cellview"; // 简单说明下想对cell直接丢text[虽然我估计你不会这么操作,而且这么操作会让子VIEW不显示] // 是无法使用cell.text的 // 而是使用cell viewWithTag:x // 这里很有趣cell能通过自己的viewWithTag 来获取自己顶层的view[此view无法循环viweWithTag]。然后进行操作。 // UITableViewCell * b=((UITableViewCell *)[cell viewWithTag:10]); // ((UILabel *)[b viewWithTag:1]).text=@"so funy"; //赋值 cell.idlb.text=swearVO.swearId; cell.describe.text=swearVO.swearIntroduce; cell.button.tag=[swearVO.swearId integerValue]; if([self.fireImages objectAtIndex:[swearVO.fireLever integerValue]-1]){ cell.fire.image=[self.fireImages objectAtIndex:[swearVO.fireLever integerValue]-1]; } //计算cell变化 start int line; if(swearVO.swearName.length%rowSize){ line =(swearVO.swearName.length/rowSize)+1; }else{ line=(swearVO.swearName.length/rowSize); } NSString *str=swearVO.swearName ; CGSize size=[str sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(210.0f,1000.0f ) lineBreakMode:NSLineBreakByWordWrapping]; CGSize size2=[@"1" sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(210.0f,1000.0f ) lineBreakMode:NSLineBreakByWordWrapping]; //name frame修改 CGRect rect=cell.name.frame; rect.size.height=size.height; int move=size.height-size2.height; cell.name.frame=rect; //name内容增加"\n"换行符 那个方法用了递归 cell.name.text=str; //修改Dscribe位置 CGRect rectDscribe; rectDscribe=cell.describe.frame; // NSLog(@"%f!!!!!!!!",rectDscribe.origin.y); rectDscribe.origin.y=DEFAULTDSCRIBE+move; cell.describe.frame=rectDscribe; // cell.name.bounds=rectBound; // NSLog(@"%d|%f|%f",[cell.name numberOfLines], cell.name.bounds.size.height, cell.name.frame.size.height); // labCountry.text=swearVO.countryId; // labName.text=swearVO.swearName; // labLevel.text=swearVO.fireLever; // labdescribe.text= cell.describe.text=swearVO.swearIntroduce; // bt.tag=[swearVO.swearId integerValue]; // if(row%2){ // cell.contentView.backgroundColor=[UIColor yellowColor]; // }else{ // cell.contentView.backgroundColor=[UIColor yellowColor]; // } return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [self CountryNameBySession:section]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat height; NSInteger countryID=[self getCountryIdBySession:indexPath.section]; NSArray *array=[[self getArrayByCountryId:countryID] mutableCopy]; SwearVO *swearVO=(SwearVO *)[array objectAtIndex:indexPath.row]; int line=(swearVO.swearName.length/(rowSize+1))+1; height=BASEHEIGHT+(line-1)*PERHEIGHT; return height; } /* // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */ /* // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */ /* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */ #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self srceenShot]; NSInteger countryID=[self getCountryIdBySession:indexPath.section]; self.array=[[self getArrayByCountryId:countryID] mutableCopy]; SwearVO *swearVO=(SwearVO *)[self.array objectAtIndex:indexPath.row]; [self shareWithText:swearVO.swearName]; } - (void)shareWithText:(NSString *)text{ NSArray *activityItems; self.text=text; if(self.image){ activityItems=@[self.text,self.image]; }else{ activityItems=@[self.text]; } UIActivityViewController *avc=[[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; [self presentViewController:avc animated:YES completion:nil]; } - (void)srceenShot{ // UIGraphicsPushContext(UIGraphicsGetCurrentContext()); UIGraphicsPushContext(UIGraphicsGetCurrentContext()); UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; self.image=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIGraphicsPopContext(); // UIGraphicsPopContext(); } - (IBAction)btVideoPressed:(UIButton *)sender { // UITableViewCell *cell=(UITableViewCell *)sender.superview; NSLog(@"%d",sender.tag); } -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ //用于设置sectionIndexTitle //返回要为一个内容为NSString 的NSArray 里面存放section title; //默认情况下 section Title根据顺序对应 section 【如果不写tableView: sectionForSectionIndexTitle: atIndex:的话】 NSMutableArray* a=[NSMutableArray array]; for(CountryVO *c in self.countryDy){ [a addObject: [c.countryName substringToIndex:1]]; } // self.tableView.sec // self.tableView.sectionIndexTrackingBackgroundColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; // return b=@[@"1",@"2"]; return a; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ //传入 section title 和index 返回其应该对应的session序号。 //一般不需要写 默认section index 顺序与section对应。除非 你的section index数量或者序列与section不同才用修改 return index; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIImageView *result=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"title_bg.png"]]; UILabel *content=[[UILabel alloc]initWithFrame:CGRectZero]; content.frame = CGRectMake(10.0, 0.0, 300.0, 18.0); content.font=[UIFont systemFontOfSize:15]; content.backgroundColor=[UIColor clearColor]; content.textColor=[UIColor whiteColor]; content.text=((CountryVO *)[self.countryDy objectAtIndex:section]).countryName; [result addSubview:content]; return result; } //根据传入的string增补 "\n" //注:【在stroyboard里面设置lable行数为0,既可自动换行】 //-(NSString *)stringWillNByString:(NSString *)str line:(NSInteger)lineNum{ // NSString *firstStr; // NSString *secondStr; // if(str.length>rowSize){ // firstStr=[str substringToIndex:rowSize]; // firstStr=[firstStr stringByAppendingString:@"\n"]; // secondStr=[str substringFromIndex:rowSize]; // [self stringWillNByString:secondStr line:lineNum-1]; // return [NSString stringWithFormat:@"%@%@",firstStr,secondStr]; // }else{ // secondStr=str; // return secondStr; // } // // //} @end
发表评论
-
[转]使用CGContextDrawImage绘制图片上下颠倒
2014-08-03 23:39 8314转自:http://www.bennychen.cn/ta ... -
[ios]ios-Demo4脱衣服/刮奖app-专业
2014-07-31 21:38 3171普通版本完成的锯齿很严重但是Ios系统中仅CGContex ... -
[ios]ios脱衣服/刮奖app-简单版
2014-07-31 12:21 1274其实就是两张图片,然后第一张图片被手指划过的地方clear ... -
[ios]视差动画
2014-07-16 09:19 928http://www.teehanlax.com/blog/ ... -
[ios]label增加边距-既左右对齐
2014-07-09 01:23 3425http://www.cocoachina.com/bbs/ ... -
[ios] iOS 富文本类库RTLabel
2014-06-30 16:09 1598转自:http://blog.csdn.net/duxinf ... -
[ios]CA animationWIthKeyPath的一些思考
2014-05-13 19:05 2325使用CABasicAnimation的时候 总会遇到 an ... -
[ios]UIVIew的初始化执行方法
2014-04-03 14:56 1530根据你需要执行的动作, 这里有几个方法: -(id)init ... -
[ios]Gesture与scrollView分别实现图片触控
2014-04-02 21:53 1249ScrollView方法 - (void)viewDidL ... -
[ios]Gesture与scrollView分别实现图片触控
2014-04-02 21:53 1... -
[ios]block写法
2014-04-02 21:46 1040参考:http://www.cnblogs.co ... -
[ios]Block分离DataSource -ios升级日记2
2014-04-02 21:44 802根据:http://objccn.io/issue-1-1 ... -
[ios]快速打印
2014-04-02 16:04 588以前打印frame老是用fram.x.x这样 太慢。 其实 ... -
[ios]自定义圆形滑动条-ios升级日记1
2014-04-01 21:53 6604学习下自定义控件。从开源项目中挖了一个 理解模仿了一个 ... -
[ios]iOS7状态栏隐藏
2014-04-01 10:39 670在iOS7中 ,如果你还想用UIApplication ... -
OC简写
2014-03-31 19:43 1033简写 OC的语法一直被认为比较麻烦,绝大多数的 ... -
[ios]NSNotification
2014-01-09 17:48 1575这是一个观察者模式。 [[NSNotific ... -
[ios]注销键盘
2014-01-09 17:17 767[[UIApplication sharedApplica ... -
[ios]GCD-Grand Central Dispatch - 2
2014-01-02 13:04 2554Grand Central Dispatch =GDC1.有两 ... -
[ios] NSLocale 获取当前语言版本
2013-12-02 17:08 2598获取当前手机情况NSLocale 获取当前语言版本: ...
相关推荐
IOS开发 tableview中cell的用法 值得拥有
swift ,使用autolayout + storyboard 最外层tableview列表,cell里面嵌套了tableview,自适应内容,cell里的tableview不可滑动,内容全部显示,且文字分行显示,不用设置cell的高度直接可以自适应内容
点击cell上面的按钮进行当前cell的展开和收缩,并只进行当前行的刷新。
TableView的Cell上播放视频
iOS TableView 添加手势方法
tableview cell 倒计时
ios tableview 异步 加载图片
tableview手动适应cell高度,以及iOS8之后自动适应cell高度
iOS tableview 展开与收回
ios tableView 利用run Loop 进行懒惰加载 很多人 不知道怎么利用runLoop 处理cell 我这个专门做了一个小demo 处理了一下 分享给 想学习的人
ios tableview 31个源码 ,什么类型的基本上都包括了,代码也很容易理解,赞,需要的下
iOS TableView 下拉图片放大 上拉图片高度缩小 TableViewFrame动态变化
通过button的点击事件,完成对cell个数的增加和减少
Ios tableview下拉菜单 展开收起
swift tableview 每个cell点击展开点击折叠,展开的时候可以填写信息(textfield),刷新tableview不受影响,实时获取到cell上填写的信息数据
iOS tableview 索引
iOS mvc 模式 写了个 tableview demo 新手记录,有不足之处,见谅
表格的隐藏与展示,成熟的Demo,逻辑以及思路都在文件中写有注释,应该还是比较好理解的,和服务器的数据对接也比较容易。
非常漂亮的 tableview站来折叠效果