`

UITableView小结

 
阅读更多

 

UITableViewDataSource

 

#pragma mark - UITableViewDataSource
//一共有多少组(可以不写,默认为1组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}
//每个组有多少数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
//每组数据如何显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell_ABC";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] ;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //使用xib自定义时使用
        //cell = [[[NSBundle mainBundle] loadNibNamed:@"XYContentCell" owner:self options:nil] lastObject];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    
    cell.textLabel.text = @"123";
    
    
    return cell ;
}

 

 

 

获取UITableView 内部高度(Content Height)

 

- (CGFloat)tableViewHeight {
    [tableView layoutIfNeeded];
    return [tableView contentSize].height;
}

 

 

UITableView 某一行 默认是 选中 状态

NSIndexPath *index0 = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:index0
                            animated:YES
                      scrollPosition:UITableViewScrollPositionBottom];

 

UITableView 滑动到指定位置

 

NSArray *visibleRows = [self.mTableView indexPathsForVisibleRows];
        if (visibleRows.count > 0) {
            NSIndexPath *visibleIndexPath = visibleRows[0];
            if (visibleIndexPath.row == indexPath.row) {
                //滑动到 指定位置
                [self.mTableView scrollToRowAtIndexPath:indexPath
                                       atScrollPosition:UITableViewScrollPositionTop
                                               animated:YES];

 

 

UISwitch in a UITableView cell (UITableViewCell里镶嵌UISwitch)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch( [indexPath row] ) {
        case MY_SWITCH_CELL: {
            UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
            if( aCell == nil ) {
                aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
                aCell.textLabel.text = @"I Have A Switch";
                aCell.selectionStyle = UITableViewCellSelectionStyleNone;
                UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
                aCell.accessoryView = switchView;
                [switchView setOn:NO animated:NO];
                [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
                [switchView release];
            }
            return aCell;
        }
            break;
    }
    return nil;
}

- (void) switchChanged:(id)sender {
    UISwitch* switchControl = sender;
    NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}

 

 

自定义UITableView的Header

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 20.0)];
    
    UIImageView *bg = [[UIImageView alloc]initWithFrame:customView.frame];
   
    bg.image = [UIImage imageNamed:@"carTypeCellTitleBg1.png"];
  
    
    [customView addSubview:bg];
    
    // create the button object
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    
    headerLabel.backgroundColor = [UIColor clearColor];
    
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor colorWithRed:242.0/255.0f green:161.0/255.0f blue:4.0/255.0 alpha:1.0];
    
    //	headerLabel.highlightedTextColor = [UIColor whiteColor];
    
    headerLabel.font = [UIFont italicSystemFontOfSize:15];
    headerLabel.frame = customView.frame;
    
    // If you want to align the header text as centered
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
    
    //	headerLabel.text = <Put here whatever you want to display> // i.e. array element
    
    
    headerLabel.text = @"title";
    
    [customView addSubview:headerLabel];
    
    return customView;
   	
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 21.0;
}

 

 

1.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

方法要比

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法

先返回

 

即,高度比cell填充先返回

 

2.

xib自定义Cell,复用无效;需要用代码重写

 

再一次验证,xib自定义的Cell可以复用

有两种方式,

第一种:

     在自定义的Cell里面重写reuseIdentifier方法

- (NSString*)reuseIdentifier
{
    //返回的是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法里定义的reuseIdentifier
    return @"ContentCELL";
}

 

 

 第二种:

在cell的xib文件里定义

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    iphone小结

    在UITableView中,我们可以通过设置`selectedBackgroundView`来改变单元格被选中时的背景。在示例代码中,创建了一个UIView对象`myview`,设置了其frame和背景色,并将它赋值给了cell的`selectedBackgroundView`...

    从零开始学iOS7开发系列教程-事务管理软件开发实战-Chapter211

    小结 在本章节中,我们学习了如何使用NSUserDefaults来记录相关信息,判断用户是否是首次打开应用。我们还了解了基本数据类型和对象的区别,并学习了如何将基本数据类型的值转换成NSNumber对象。这些知识点对于iOS...

    iPhone开发秘籍.part2.rar

    1.14 小结.....29 第2章视图......30 2.1 UIView 和UIWindow...30 2.1.1 层次结构.....30 2.1.2 几何特征.....31 2.1.3 手势.....34 2.2 秘诀:添加递进式子视图.....34 2.3 秘诀:拖动视图.....36 2.3.1 UITouch......

    iPhone开发秘籍.part1.rar

    1.14 小结.....29 第2章视图......30 2.1 UIView 和UIWindow...30 2.1.1 层次结构.....30 2.1.2 几何特征.....31 2.1.3 手势.....34 2.2 秘诀:添加递进式子视图.....34 2.3 秘诀:拖动视图.....36 2.3.1 UITouch......

    iPhone开发秘籍.part4.rar

    1.14 小结.....29 第2章视图......30 2.1 UIView 和UIWindow...30 2.1.1 层次结构.....30 2.1.2 几何特征.....31 2.1.3 手势.....34 2.2 秘诀:添加递进式子视图.....34 2.3 秘诀:拖动视图.....36 2.3.1 UITouch......

    ios软件设计说明书

    九、小结与功能综述 软件设计应注重整体性,确保各个功能模块协调运作。同时,要充分考虑市场预期,使软件在满足用户需求的同时,具备良好的市场前景。 总结,iOS软件设计是一门融合艺术和技术的学科,需要开发者...

Global site tag (gtag.js) - Google Analytics