0

I added an UIView to the custom cell in UITableView in storyboard and I access it like this:

UIView *customView = (UIView *)[cell viewWithTag:CELL_CUSTOM_VIEW_TAG];
NSLog(@"%f", customView.frame.size.width);

But I get its width 0.0000. Am I accessing it in a wrong way?

EDIT: Here's my full cellForRow method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIImageView *customImageView = (UIImageView *)[cell viewWithTag:CELL_IMAGE_VIEW_TAG];
    customImageView.image = [UIImage imageNamed:@"foo.jpg"];
    //down is the code why I need the frame of it
    //I'm trying to make a right border to this UIImageView:
    CGRect imageViewFrame = customImageView.frame;
    CALayer *imageViewRightBorder = [self rightBorderWithColour:[UIColor blackColor] forView:imageViewFrame];
    [customImageView.layer addSublayer:imageViewRightBorder];

    return cell;
}

And here is my method for right border to the imageView:

- (CALayer *)rightBorderWithColour:(UIColor *)color forView:(CGRect)viewFrame {
    CALayer *rightBorder = [CALayer layer];
    rightBorder.frame = CGRectMake(viewFrame.size.width, 0.0f, 1.0f, viewFrame.size.height);
    rightBorder.backgroundColor = [color colorWithAlphaComponent:1.0f].CGColor;
    return rightBorder;
}
Alick Dikan
  • 65
  • 2
  • 3
  • 10

1 Answers1

2

I don't understand your logic. cellForRowAtIndexPath is where you define the UIView so technical you have it. As the other user said, show us your code.

Anyways, this is how I defined a custom UIButton in my tableview that shows up in every row. I am assuming you are trying to do something similar. Forget adding your UIView to storyboard. See if you can replicate it using this code. Of course change it to your UIView code.

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

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(timerStopStart:)
         forControlEvents:UIControlEventTouchDown];
        [button setTitle:@"Start/Stop" forState:UIControlStateNormal];
        button.frame = CGRectMake(5.0f, 40.0f, 72.0f, 77.0f);
        button.tag = (indexPath.row)+100;
        [cell addSubview:button];

        //NSLog(@"number: %d ...", (indexPath.row)+100);
    }
}
Sam B
  • 27,273
  • 15
  • 84
  • 121
  • I do that because I have a custom UITableViewCell in storyboard where I, for example, added a UIButton (because I just don't want to do that programmatically). I retrieve it by using cell viewWithTag. That's my logic – Alick Dikan Sep 02 '13 at 13:39
  • For fun try to add UIView or UIButton (whatever you are trying to add) to the table programmatically. I bet you will get to it. Trust me I have been on this road before. – Sam B Sep 02 '13 at 13:45
  • There is no need to add the `UIView` programatically. Show us the code, I think you made a small mistake, we can point out you the mistake. – Bhavin Sep 02 '13 at 13:51
  • Just a small note, instead of adding stuff to the UITableViewCell directly, you may want to add it to the contentView property instead. – David Carvalho Sep 02 '13 at 13:53