子类uitableviewcell并覆盖layoutsubviews

时间:2013-11-22 12:54:26

标签: ios objective-c uitableview

我需要在普通的UItableView中使单元格选择背景视图不在全宽度上。为此我创建了UItableViewCell子类并覆盖layoutsubviews方法

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.selectedBackgroundView.frame = CGRectMake(self.frame.origin.x + 10.0f, self.frame.origin.y, self.frame.size.width - 20.0f, self.frame.size.height);

}

我的cellForRowAtIndexPath方法看起来像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat: @"CELL %i %i", indexPath.section, indexPath.row];
    GroupedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil)
    {
        cell = [[GroupedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];


        UIView *cellBgView = [[UIView alloc] init];
        cellBgView.frame = CGRectMake(10, 0, 300, 80);
        [cellBgView setBackgroundColor:[UIColor colorWithRed:242 / 255.0 green:242 / 255.0 blue:242 / 255.0 alpha:1.0]];
        [cell.contentView addSubview:cellBgView];
    }



        [cell setBackgroundColor:[UIColor clearColor]];
    UIView *selectionView = [[UIView alloc] init];
    [selectionView setBackgroundColor:[UIColor colorWithRed:181 / 255.0 green:211 / 255.0 blue:53 / 255.0 alpha:1.0]];
    selectionView.frame = CGRectMake(10, 0, 300, 80);
    cell.selectedBackgroundView = selectionView;
    return cell;
} 

但只有第一行selectedView才能正常工作。对于其他行,我选择了具有清晰颜色的View。求你帮帮我。

2 个答案:

答案 0 :(得分:4)

我只需要用数字设置框架。

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.selectedBackgroundView.frame = CGRectMake(10.0f, 0, 300, 80);

}

答案 1 :(得分:2)

而是在子类化单元格中添加背景, 这样做是一个例子,你可以如何管理单元格中的选择和取消选择状态,根据您的要求进行更改

   //in your subclassed cell
   #import "GroupedTableCell.h"

   @implementation GroupedTableCell

   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
       // Initialization code
       //add the background view hear only, changes the frame in the layoutsubviews
       UIView *cellBgView = [[UIView alloc] init];


     [cellBgView setTag:12345];//using tag to access in the layoutsubviews
     [self addSubview:cellBgView];//hear u added the background view



    }
    return self;
 }

 //manage the cell selection and deselection state hear
  - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   // Configure the view for the selected state  
   [super setSelected:selected animated:animated];
    UIView *cellBgView = [self viewWithTag:12345];
    if(selected)
    {

      [cellBgView setBackgroundColor:[UIColor colorWithRed:242 / 255.0 green:242 / 255.0 blue:242 / 255.0
                                                   alpha:1.0]]; //your selected background color
    }
    else
   {
       [cellBgView setBackgroundColor:[UIColor clearColor]]; //your deselected background color

   }

}

//setting the frames of views within the cell
- (void)layoutSubviews
{
    [super layoutSubviews];
    UIView *cellBgView = [self viewWithTag:12345];
    cellBgView.frame = CGRectMake(10, 0, 300, 80);//always set the frame in layoutSubviews

}


控制器中的

就是这样做的


  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      GroupedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
      if(cell == nil)
      {
          cell = [[GroupedTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
      }

     return cell;

 }

希望这有助于你:)

相关问题