iphone常用控件之UITableView 基本语句

本文介绍如何使用UITableView并实现自定义功能,包括高度可调的单元格、显示更多按钮及默认选中第一行等特性。
    
//高度可调且有一个显示更多的按钮

-(void)viewDidLoad{ UITableView* table=[[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 416)]; table.delegate=self; table.dataSource=(id)self; [self.view addSubview:table]; [table release];

}
#pragma mark Table View Data Source Methods
//对应section的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray count];   //这里利用数组计数
}
//点击列表中某一行触发的对应操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{NSLog(@"row si %d",row);
} 
//每个cell高度不一样
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if([indexPath row]<[dataArray count])
    {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.frame.size.height;
        [cell release];
    }
    else {
        return 65;
    }
}

比较节省资源的用法:cellForView 里面重用
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    UILabel *cellTitleLabel;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        //展品名称
        cellTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(28, 20, 200, 23)];
        cellTitleLabel.backgroundColor = [UIColor clearColor];
        cellTitleLabel.textColor = [UIColor colorForHex:@"D4D2D0"];
        cellTitleLabel.tag = 100;
        [cell addSubview:cellTitleLabel];

    }
    //未点中状态cell背景
        cellTitleLabel = (UILabel *)[cell viewWithTag:100];
        titleString = [NSString stringWithFormat:@"%@",[[dataArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
        cellTitleLabel.text = titleString;
return cell;

}
 
//默认选中第一行 
NSIndexPath *ip=[NSIndexPathindexPathForRow:0inSection:0];    
[table selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionBottom]; 

转载于:https://www.cnblogs.com/ios-wmm/archive/2012/08/20/2647837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值