Davis
Davis

Reputation: 1263

Change width of TableView or TableViewCell

Hey everybody :) i try to change the width of my cells so there is a little space between the cells and the tableview border. I tried everything i could found here on stackoverflow, but without success.

I created the tableview with the interface builder, so first i simple tried to set the tableview size to "freeform" and dragged the width to 300.0f, but nothing happenend. Than i tried to do it programmatically in my "viewDidLoad" with:

self.tableView.frame = CGRectMake(10.0f, self.tableView.frame.origin.y, 300.0f, self.tableView.frame.size.height);

but here also nothing happens.... than i tried to change the cells directly with:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

newsCell.contentView.frame = CGRectMake(10.0f, 0, 300, newsCell.frame.size.height);

}

but same Problem here....any ideas what I missing?

EDIT: Another Solution for this Problem is to change the frame of the Custom Cell with:

- (void)setFrame:(CGRect)frame {
    frame.origin.x += inset;
    frame.size.width -= 2 * inset;
    [super setFrame:frame];
}

Upvotes: 1

Views: 9797

Answers (4)

Shankar BS
Shankar BS

Reputation: 8402

just try this in your custom cell put a property like

   in .h file
   @interface GTNewsCustomCell : UITableViewCell
   @property (nonatomic, assign)CGRect cellFrame;

   in .m file
   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
   { 
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
       // Initialization code
       self.backgroundColor = [UIColor greenColor];//for testing purpose only
   } 
   return self;
 }

  - (void)setSelected:(BOOL)selected animated:(BOOL)animated
   {
       [super setSelected:selected animated:animated];

       // Configure the view for the selected state
   }

    //automatically called 
   - (void)layoutSubviews
  {
      [super layoutSubviews];
       CGRect cellRect = self.bounds;
       cellRect.size.width = self.cellFrame.size.width;

       self.bounds = cellRect;

  }

   .in .m of viewController

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if(cell == nil)
        {
            cell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
       cell.cellFrame = CGRectMake(10, 0, tableRect.size.width,40);//hear tableRect is the frame of your tableview
       return cell;
   }

not sure try this hope this helps u

Upvotes: 4

A J
A J

Reputation: 605

I think you want dynamic Height for the Tableviewcell instead of width.

Delegate method of UITableView will help on this:

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

Which will return height of every cell. You can implement it as following sample code. This is showing dynamic height on the basis of dynamic text content.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  //set width depending on device orientation
  self.cellPrototype.frame = CGRectMake(self.cellPrototype.frame.origin.x,     self.cellPrototype.frame.origin.y, tableView.frame.size.width, self.cellPrototype.frame.size.height);

 CGFloat quotationLabelHeight = [self sizeOfLabel:self.cellPrototype.quotationLabel withText:[self quotationTextForRow:indexPath.row]].height;
CGFloat attributionLabelHeight = [self sizeOfLabel:self.cellPrototype.attributionLabel withText:[self attributionTextForRow:indexPath.row]].height;
CGFloat padding = self.cellPrototype.quotationLabel.frame.origin.y;

CGFloat combinedHeight = padding + quotationLabelHeight + padding/2 + attributionLabelHeight + padding;
CGFloat minHeight = padding + self.cellPrototype.avatarButton.frame.size.height + padding;

return MAX(combinedHeight, minHeight);
}

You can try with this too.

Upvotes: 0

Ashutosh
Ashutosh

Reputation: 2225

For this, first of all you can take an UIImageView to cover your full view and set its image as a bordered image. Now add a table view on this imageview with making width so as the borders of this image is visible.

Upvotes: 1

rahul
rahul

Reputation: 178

Use this:

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

delegate method of UITableView and return a float value (CellHight+space bw cells).

Upvotes: -2

Related Questions