iPhone开发学习笔记003——UITableViewCell内容自适应高度

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 16:07 点击:
此篇使用前一篇http://www.2cto.com/kf/201202/120764.html
的实例,只是将对应的Cell.xib界面换成如下所示:


\


然后,oneViewController中添加属性:dataArray,即 *.h中添加@property (nonatomic,retain)NSArray *dataArray;
*.m中添加:@synthesize dataArray;
oneViewController.m中的部分代码如下:


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataArraycount];
}

 


// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   

    static NSString *CustomCellIdentifier = @"CellIdentifier";


    UITableViewCell *cell = [tableView

dequeueReusableCellWithIdentifier: CustomCellIdentifier];

    if (cell ==nil) {

        NSArray *nib = [[NSBundlemainBundle]loadNibNamed:@"Cell"

owner:selfoptions:nil];

        //        if ([nib count] > 0) {

        //            cell = self.tableViewCell;

        //        } else {

        //            NSLog(@"failed to load CustomCell nib file!");

        //        }

        cell = [nib objectAtIndex:0];

    }

    NSUInteger row = [indexPathrow];

   

NSLog(@"++++++++++++++ jonesduan %s, cell row:%d", __func__, row);

   

    UILabel *cellLabel = (UILabel *)[cellviewWithTag:1];

    cellLabel.text = [NSStringstringWithFormat:@"cell index %d:", row];

   

    UILabel *contentLabel = (UILabel *)[cellviewWithTag:2];

   

    NSDictionary *rowData = [self.dataArrayobjectAtIndex:row];

   

    //设置自动行数与字符换行 

    [contentLabel setNumberOfLines:0]; 

    contentLabel.lineBreakMode =UILineBreakModeWordWrap;

   

    UIFont *font = [UIFontfontWithName:@"Arial"size:12];

   

   //设置一个行高上限 

    //CGSize size = CGSizeMake(100 /*nameLabel.frame.size.width*/, 2000);

    CGSize size = CGSizeMake(100,2000); // 这里不一定就得是100和2000,具体数字是自已试出来的,这里的修改会影响下面计算出来的labelsize ,望牛人指教此处两个参数应该如何确定具体值,即能够通过计算一次得出应该设置的值???

    NSLog(@"nameLabel(x, y, witdh, height) = (%f, %f, %f, %f)", contentLabel.frame.origin.x, contentLabel.frame.origin.y, contentLabel.frame.size.width, contentLabel.frame.size.height);

    NSLog(@"size(width, height) = (%f, %f)", size.width, size.height);

   

    NSString *labelText = [rowDataobjectForKey:@"Name"];

   

   //计

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • 如何为Iphone应用创建启动界面
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1