1
votes

i need to have 2 kinds of rows. One with 44 px of height and other with 90px:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath   *)indexPath {
    if (indexPath.section == 0) 
        return 90.0;
    else
        return 44.0;
}

The problem is when i called - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath because i need to add to the cell with 90px an scrollView that have 90px of height but it's added on the middle of the cell instead on the top.

 if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:ScrolledCellIdentifier];
        if (cell == nil)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ScrolledCellIdentifier];
        //cell.textLabel.text = @"hi";

        [cell addSubview:self.scrollView];
    }

Trying to set the textLabel of the cell works, the problem is adding the scrollView.

If i add this line to the code:

self.scrollView.frame = CGRectMake(0,-45, 320, 90);

It works perfectly but it sounds some weird for me. What is happening?

3
Just to clarify - why do you need to adjust the frame of the scrollView?theTRON
because if i don't adjust the frame it takes as height 44 px thanks!xger86x

3 Answers

1
votes

Set the frame of your scrollView to the bounds of your cell before you add it.

So

self.scrollView.frame = cell.bounds;
[cell addSubview:self.scrollView];

Debug... NSLog.... output the frames of things so you can see where they are getting positioned as opposed to where you want them to be positioned. Use this for outputting frame coordinates:

NSLog(@"Frame is: ", NSStringFromCGRect(self.scrollView.frame));

or define a nice shortcut, as I use this all the time

#define LogCGRect(rect) NSLog(@"CGRect:%@", NSStringFromCGRect(rect));

then you can just do

LogCGRect(self.scrollview.frame);
1
votes

It just fits the scrollview into the tableviewcell, no matter if it's height is 44 or 90.

1
votes

I solved my problem using this code:

cell = [tableView dequeueReusableCellWithIdentifier:ScrolledCellIdentifier];
if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ScrolledCellIdentifier];
cell.frame = CGRectMake(0, 0, 320, 90);
self.scrollView.frame = CGRectMake(0,0, 320, 90);
[cell addSubview:self.scrollView];

i don't know if it's wrong but it works...