JordanD
JordanD

Reputation: 163

TableView Behaving Weirdly

My tableView (phpush) behaves very unusually. The cell from section 0 randomly appears at different rows, and sometimes disappears from the top of the table. Here are some methods that I think may be causing the problem:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (tableView==_phpush) {

        if (indexPath.section==0) {

            UIButton *pushButtonDate;
            UIButton *pushButtonPush;
            UIButton *edit;

            if (cell == nil) {

                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                pushButtonPush = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [pushButtonPush addTarget:self action:@selector(pushButtonPushPressed:)forControlEvents UIControlEventTouchUpInside];
                [pushButtonPush setTitle:@"Push-Ups" forState:UIControlStateNormal];
                pushButtonPush.frame = CGRectMake(150,7,97,30);
                pushButtonDate.tag = 3;
                [cell.contentView addSubview:pushButtonPush];

                pushButtonDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [pushButtonDate addTarget:self action:@selector(pushButtonDatePressed:)forControlEvents: UIControlEventTouchUpInside];
                [pushButtonDate setTitle:@"Date" forState:UIControlStateNormal];
                pushButtonDate.frame = CGRectMake(11,7,97,30);
                pushButtonDate.tag = 4;
                [cell.contentView addSubview:pushButtonDate];

                edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [edit addTarget:self action:@selector(editButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
                [edit setTitle:@"Edit" forState:UIControlStateNormal];
                edit.frame = CGRectMake(200,7,97,30);
                edit.tag = 5;
                [cell.contentView addSubview:edit];
            }
            else {
                pushButtonPush = [cell.contentView viewWithTag:(3)];
                pushButtonDate = [cell.contentView viewWithTag:(4)];
                edit = [cell.contentView viewWithTag:5];
            }
            return cell;
        }
        else {
            UILabel *pushLabel;
            UILabel *pushLabel2;
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                pushLabel = [[UILabel alloc]initWithFrame:CGRectMake(150,7,97,30)];
                pushLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10,7,150,30)];
                NSArray *array;
                if (sortByDate==true) {
                    array = [[pushDictionary allKeys]sortedArrayUsingSelector
                             :@selector(localizedCaseInsensitiveCompare:)];
                }
                else {
                    array = [pushDictionary keysSortedByValueUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
                }
                pushLabel.text = [pushDictionary valueForKey:[array objectAtIndex:indexPath.row]];
                NSString *pushLabel2String = [[array objectAtIndex:indexPath.row] substringToIndex:MIN(10, [[array objectAtIndex:indexPath.row] length])];
                NSArray *pushLabel2Array = [pushLabel2String componentsSeparatedByString:@"-"];
                pushLabel2.text = [NSString stringWithFormat:@"%@/%@/%@",[pushLabel2Array objectAtIndex:1],[pushLabel2Array objectAtIndex:2],[pushLabel2Array objectAtIndex:0]];
                pushLabel.tag = 1;
                pushLabel2.tag = 2;
                [cell.contentView addSubview:pushLabel];
                [cell.contentView addSubview:pushLabel2];
            }
            else {
                pushLabel = [cell.contentView viewWithTag:1];
                pushLabel2 = [cell.contentView viewWithTag:2];
            }
            return cell;
            }
        }
    else {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        else {

        }
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView==_phpush) {
        return 2;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView==_phpush) {
        if (section==0) {
            return 1;
        }
        return  [pushDictionary allKeys].count;
    }
    return 1;
}

I'm wondering if there are any big mistakes I am making that would be causing this problem. I am pretty new to iOS programming, so please forgive me if I have made an obvious mistake. Do I need to post more of my code?

Thanks,

Jordan

Upvotes: 0

Views: 65

Answers (2)

Darren
Darren

Reputation: 10398

Cells are reused on scrolling and when a cell is reused it has all it's previous elements still in it. So if you set some text in a cell, when it's reused the text is still there, so you need to set it to nil or remove it if you don't want it. You shouldn't be allocating anything in cellForRowAtIndexPath, as when you scroll you adding more and more objects on top of the old ones.

Take a look at subclassing UITableViewCell.

Upvotes: 0

paulrehkugler
paulrehkugler

Reputation: 3271

There is a problem with your tableViewCell dequeueing.

In your else statement

else {
            pushButtonPush = [cell.contentView viewWithTag:(3)];
            pushButtonDate = [cell.contentView viewWithTag:(4)];
            edit = [cell.contentView viewWithTag:5];
     }

you never modify the contents of those buttons. Those variables you're setting are unused because you don't change the button text, or remove or add the views. If they need to do something different, you need to specify it here.

Upvotes: 1

Related Questions