开发者

UITableView problem with adding extra cell

First question on this site, although I have been around for a while behind the scenes. I have a problem that I have been racking my head on for the last two days and I hope someone can shed some light on it for me.

I have a UITableView, which is loaded from a SQL database. It has 15 entries in it. I have inserted an extra cell at the beginning of the UITableView. This extra cell is for a UITextField and UIButton which adds an item into the database.

When the view is loaded, the first cell with the custom objects shows fine, and the rest of the table is filled with items from the database and looks just how it should. However, when the UITableView is scrolled down so the first cell is out of view, then back up, it takes the value of the 11th row item and shows it over top the first cell.

Here is my code:

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

static NSString *CellIdentifier = @"Cell";

static NSInteger NameTag = 1;


UITableViewCell *cell = [popTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];


    CGRect frame;

    frame.origin.x =50;

    frame.origin.y =10;

    frame.size.height =22;

    frame.size.width =275;


    UILabel *nameLabel = [[UILabel alloc] initWithFrame:frame];

    nameLabel.tag = NameTag;

    nameLabel.opaque = YES;

    nameLabel.textColor = [UIColor grayColor];

    nameLabel.backgroundColor = [UIColor clearColor];

    [cell.contentView addSubview:nameLabel];

    [nameLabel release];


}

int row = [indexPath row];
if (row == 0) {

    UIButton *buttonLe开发者_如何转开发ft = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLeft setFrame: CGRectMake( 205, 6, 40, 33)];
    [buttonLeft addTarget:self action:@selector(addToList:) forControlEvents:UIControlEventTouchUpInside];

    [cell addSubview:buttonLeft];

//No Alloc for txtField, it is built in IB
    [txtField setBorderStyle:UITextBorderStyleNone];
    [txtField setFrame: CGRectMake( 17, 12, 180, 23)];
    txtField.backgroundColor = [UIColor clearColor];
    [txtField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    txtField.clearButtonMode = UITextFieldViewModeAlways;


}else{

    UILabel * nameLabel = (UILabel *) [cell.contentView viewWithTag:NameTag];

    Add *theObj = [self.theArray objectAtIndex:indexPath.row - 1];

    [nameLabel setText:theObj.itemName];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;



    UIImageView *imageView = [cell viewWithTag:kTagCellImageView];
    if (imageView == nil) {
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0, 10.0, 13.0, 25.0)];
        imageView.backgroundColor = [UIColor clearColor];
        imageView.tag = kTagCellImageView;
        [cell.contentView addSubview:imageView];

    }


    if([theObj.itemName isEqualToString:@"First Street"]){
        imageView.frame = CGRectMake(14,10,13,25);
        [imageView setImage:[UIImage imageNamed:@"firststreet"]];
    }
    else if([theObj.itemName isEqualToString:@"Second Street"]){
        imageView.frame = CGRectMake(8,12,29,20);
        [imageView setImage:[UIImage imageNamed:@"second"]];
    }
    else if([theObj.itemName isEqualToString:@"Main Street"]){
        imageView.frame = CGRectMake(15,10,13,25);
        [imageView setImage:[UIImage imageNamed:@"mainstreet"]];
    }
    else{

        imageView.frame = CGRectMake(8,8,25,25);
        [imageView setImage:[UIImage imageNamed:@"iconcustom"]]; 

    }



    NSLog(@"%@",itemName);
    NSLog(@"%@",itemCategory);


   }

return cell;

}

Also here is my cellForRow:

- (NSInteger)tableView:(UITableView *)popTableView numberOfRowsInSection:(NSInteger)section {

return [self.theArray count] + 1; //Add Extra cell to beginning

}

Any ideas would be greatly appreciated.


You need to use a different reuseIdentifier for your first cell. Try this:

NSString *cellIdentifier;
if (indexPath.row == 0) {
    cellIdentifier = @"first";
} else {
    cellIdentifier = @"not first";
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
// .. cell initialization
}


Obligatory tangential answer - have you thought about setting the tableHeaderView on the UITableView instead? I think that'd accomplish what you're trying to do in a cleaner way (in that it adds an arbitrary view to the top of the table).

Just create a UIView with your "add a new item" controls in it and then set it as the header view when first creating the table.


The issue is here

static NSString *CellIdentifier = @"Cell";

static NSInteger NameTag = 1;


UITableViewCell *cell = [popTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

you are dequeueing all of the cells with the same identifier. Row 1 (index 0) needs to have its own CellIdentifier. Also it looks like you keep adding subviews to the same cells that you dequeue. On your if(cell == nil) check you may want to decide if you want to remove all of the cells contentView subviews or reuse them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜