`

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文件里定义

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics