2011-03-29 61 views
0

本网站的第一个问题,虽然我已经在幕后花了一段时间。我有一个问题,在过去的两天里,我一直在嘲笑我,并希望有人能为我揭示一些事情。添加额外单元格的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 

}

任何想法,将不胜感激。

回答

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 
} 
+0

工作完美,谢谢! – Ryan 2011-03-29 18:54:38

1

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

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

+0

这可能是我会做的。要么把它放在tableHeaderView或tableView:viewForHeaderInSection: – 2011-03-29 18:22:02

+0

谢谢,甚至没有想到会在下次记住它! – Ryan 2011-03-29 18:56:31

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子视图或重用它们。

+0

感谢您的回答,我非常感谢。我也会检查子视图问题,并找出最佳效果。再次感谢。 – Ryan 2011-03-29 18:59:38