iphone常用控件之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]; 
posted @ 2012-08-20 17:04  小、  阅读(382)  评论(0编辑  收藏  举报