添加额外单元格的UITableView问题

时间:2011-03-29 17:54:26

标签: iphone uitableview

这个网站上的第一个问题,虽然我已经在幕后停留了一段时间。我有一个问题,我过去两天一直在讨厌,我​​希望有人可以为我解释一下。

我有一个UITableView,它是从SQL数据库加载的。它有15个条目。我在UITableView的开头插入了一个额外的单元格。这个额外的单元格用于UITextField和UIButton,它将一个项目添加到数据库中。

加载视图时,带有自定义对象的第一个单元格显示正常,表格的其余部分填充了数据库中的项目,并查看它应该如何显示。但是,当向下滚动UITableView以使第一个单元格不在视图中,然后备份时,它将获取第11行项目的值并将其显示在第一个单元格的顶部。

这是我的代码:

- (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 *buttonLeft = [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;

}

这也是我的cellForRow:

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

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

}

非常感谢任何想法。

3 个答案:

答案 0 :(得分:2)

您需要为第一个单元格使用不同的reuseIdentifier。试试这个:

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
}

答案 1 :(得分:1)

强制性的切向答案 - 您是否考虑过在UITableView上设置tableHeaderView?我认为这会以更清洁的方式完成你想要做的事情(因为它会在表格顶部添加任意视图)。

只需创建一个带有“添加新项目”控件的UIView,然后在第一次创建表时将其设置为标题视图。

答案 2 :(得分:0)

问题在这里

static NSString *CellIdentifier = @"Cell";

static NSInteger NameTag = 1;


UITableViewCell *cell = [popTableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

您将所有具有相同标识符的单元格排队。第1行(索引0)需要有自己的CellIdentifier。此外,您似乎继续将子视图添加到您出列的相同单元格。在if(cell == nil)检查中,您可能想要决定是否要删除所有单元格contentView子视图或重复使用它们。