IOS table动态cell - 笔记

来源:互联网 发布:韩国烤肉知乎 编辑:程序博客网 时间:2024/06/02 01:14
////  LPViewController.m//  temp////  Created by linpeng on 14-6-13.//  Copyright (c) 2014年 linpeng. All rights reserved.//#import "LPViewController.h"@interface LPViewController ()<UITableViewDataSource,UITableViewDelegate>{    UITableView *_table;    NSMutableArray *_dataList;    UIView *footView;}@end@implementation LPViewController- (void)viewDidLoad{    [super viewDidLoad];    _table=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];    _table.delegate=self;    _table.dataSource=self;    //_table.multipleTouchEnabled=YES;    [self.view addSubview:_table];    _dataList=[[NSMutableArray alloc]initWithObjects:@"1111\n111\n111\n111\n",@"22gdhajs大神进口到哈市好久打卡收款单号卡就是的空间安徽省科技等哈可节省空间和思考接电话开机和登记卡斯柯达灰色空间和空间撒的空间数据库等会教科书的教科书科技等哈收到空间撒接口等哈的空间阿红的空间安徽省科技等哈上课的话2",@"333",@"444",@"555",@"666",@"777", nil];    footView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];    footView.backgroundColor=[UIColor yellowColor];    _table.tableFooterView=footView;}#pragma mark - UITableView-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [_dataList count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {        static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];        label.tag = 1;        //label.lineBreakMode = UILineBreakModeCharacterWrap;        label.lineBreakMode=NSLineBreakByCharWrapping;        label.highlightedTextColor = [UIColor whiteColor];        label.numberOfLines = 0;        label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制        label.backgroundColor = [UIColor clearColor];        [cell.contentView addSubview:label];            }        UILabel *label = (UILabel *)[cell viewWithTag:1];    NSString *text;    text = [_dataList objectAtIndex:indexPath.row];    CGRect cellFrame = [cell frame];    cellFrame.origin = CGPointMake(0, 0);        label.text = text;    CGRect rect = CGRectInset(cellFrame, 2, 2);    label.frame = rect;    [label sizeToFit];    if (label.frame.size.height > 46) {        cellFrame.size.height =  label.frame.size.height+4;    }    else {        cellFrame.size.height = 50;        label.frame = CGRectMake(label.frame.origin.x, (50-label.frame.size.height)/2, label.frame.size.width, label.frame.size.height);    }    [cell setFrame:cellFrame];        return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];    return cell.frame.size.height;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

0 0
原创粉丝点击