Question

Basically, I have a table view with 2 buttons being rendered into the top two rows, I've done this through CGRectMake's and adding the buttons to the cell subview. The table view also has a search bar with a scope bar.

The problem I'm having is that when the app first loads, the buttons are shorter than I want them to be and have set them to be (almost as if they are being "defaulted" when being rendered). When I Change the scope the buttons change to the size I've set, but when I change back, the buttons don't go back to the size they were before.

The buttons are being created in the cellForRowAtIndexPath method, although I can't see how this could be the problem with the size, I do have a response problem with the scope bar (but I know this is most likely down to everything that's inside the cellForRowAtIndexPath is being called everytime the scope changes so it will naturally cause the app to be slow.

The cellForRowAtIndexPath method is here:

-- Edited 26/09/2012 After First Answer --

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *normalCellIdentifier = @"normalCell";
    static NSString *topRowCellIdentifier = @"topRowCell";

    UITableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier];
    if (normalCell == nil) {
        normalCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellIdentifier];
    }

    UITableViewCell *topRowCell = [tableView dequeueReusableCellWithIdentifier:topRowCellIdentifier];
    if (topRowCell == nil) {
        topRowCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topRowCellIdentifier];

        // Added in here
        button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        CGRect buttonRect;

        button1.tag = indexPath.row;
        button2.tag = indexPath.row;

        button1.titleLabel.tag = 0;
        button2.titleLabel.tag = 1;

        [button1 setTitle:@"Button 1" forState:UIControlStateNormal];
        [button2 setTitle:@"Button 2" forState:UIControlStateNormal];

        [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button2 addTarget:self action:@selector(buttonPressed2:) forControlEvents:UIControlEventTouchUpInside];

        [topRowCell addSubview:button1];
        [topRowCell addSubview:button2];

        buttonRect = CGRectMake(0.0f, 0.0f, topRowCell.frame.size.width / 2.0f, topRowCell.frame.size.height);

        button1.frame = CGRectMake(7.0f, 7.0f, buttonRect.size.width - 14.0f, buttonRect.size.height- 14.0f);
        button2.frame = CGRectMake(buttonRect.size.width + 7.0f, 7.0f, buttonRect.size.width - 14.0f, buttonRect.size.height - 14.0f);

        NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.avicode.co.uk/iphone/minepedia/plists/featured/buttons.plist"]];

        NSData *data1;
        data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button1.titleLabel.tag] objectForKey:@"button1ImageURL"]]];

        NSData *data2;
        data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button2.titleLabel.tag] objectForKey:@"button2ImageURL"]]];

        selectedIndexPathType1 = [[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button1.titleLabel.tag] objectForKey:@"type"];
        selectedIndexPathType2 = [[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button2.titleLabel.tag] objectForKey:@"type"];

        button1.imageView.image = [UIImage imageWithData:data1];
        button2.imageView.image = [UIImage imageWithData:data2];
    }

    // Configure the cell...
    if (segmentedControl.selectedSegmentIndex == 0) 
    {
        if (indexPath.section == 0) 
        {
            topRowCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return topRowCell;
        }
        else
        {
            return normalCell;
        }
    }
    else
    {
        return normalCell;
    }
}

Let me know if there is anything else you want.

Was it helpful?

Solution

I think part of your problem is that you're creating and adding the buttons multiple times to your cells. You should do the creation of the buttons in the then-clause of your if-statement that tests whether you successfully dequeued a previously created cell.

Another issue is that you're synchronously loading content off the internet when create the cells. If nothing else than for performance reasons you'll want to do that asynchronously using something like NSURLConnection. Use placeholder images for your buttons and replace these as you load the real images off the internet. Or, it seems like you only have two images so at least you could just cache them and not load them each time you create a cell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top