2012-03-14 26 views
6

我想实现基于UITableView的Application.For我选择UITableViewStyle是Group.In我的TableView他们是节每节有1行。为什么我们在UITableViewController中检查(cell == nil)?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 15; 
} 

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

    return 1; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section==12) 
    { 
     return 120; 
    } 
    else 
    { 
     return 60; 
    } 

} 

我想在第

添加一个UITextView对于我做下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 



    } 
    if ([indexPath section] == 12) 
    { 
     if([indexPath row]==0) 
     { 
      descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)]; 
      descriptionTextField.font = [UIFont systemFontOfSize:15.0]; 
      descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor]; 

      [descriptionTextField setDelegate:self]; 
      [descriptionTextField setTag:2]; 
      [descriptionTextField setText:@"Enter Location Description."]; 

      descriptionTextField.keyboardType=UIKeyboardTypeDefault; 
      descriptionTextField.returnKeyType=UIReturnKeyNext; 


      descriptionTextField.textColor=[UIColor blackColor]; 
      descriptionTextField.editable=YES; 
      descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords; 
      descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault; 
      descriptionTextField.textAlignment=UITextAlignmentLeft; 

      UIToolbar* keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)]; 

      UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
      UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(keyboardDoneButtonActin:)]; 
      [Done setWidth:65.0f]; 
      [keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ]; 
      [extra release]; 
      [Done release]; 

      [keboardToolBar setTintColor:[UIColor blackColor]]; 
      [keboardToolBar setAlpha:.70]; 
      [descriptionTextField setInputAccessoryView:keboardToolBar]; 
      [descriptionTextField setTag:101]; 
      [cell.contentView addSubview:descriptionTextField]; 
      [descriptionTextField release]; 
     } 
    } 

    return cell; 
} 

在initil上演这样

enter image description here

表视图

如果我滚动tableview上下,然后uitextView部分改变,它会显示多个位置。

enter image description here

我不明白我的错,为什么这个happend?

如果是实现在侧上面的代码,如果(细胞==无)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    if ([indexPath section] == 12) 
    { 
     if([indexPath row]==0) 
     { 
      **/* implemention all code here*/** 

      [cell.contentView addSubview:descriptionTextField]; 
      [descriptionTextField release]; 
     } 
    } 

    return cell; 

}

UITextView的不DISPLY,我认为是不分配。

所以是什么在 实现如果(细胞==无) { 内部 }

如果(细胞==无) { } 出侧

+0

不同之处在于,当你在单元格内写入代码== nil时,它只会在开始时工作一次,如果你写出来的话它会在每次创建时查看加载 – 2012-03-14 11:48:46

+0

为什么段值改变了,如果滚动tableview。如果我愿意代码出来? – 2012-03-14 12:02:36

+0

你可以在你的代码中做一件事,为textview设置一个标签值,然后编写这段代码[[cell.contentView viewWithTag:100 + indexPath.row] removeFromSuperview]; 100 + index.row是标记值 – 2012-03-14 12:11:35

回答

-2
NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 

// Dequeue or create a cell of the appropriate type. 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

这可以用来代替

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

这是一个简单的例子,以小区== nill

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

static NSString *CellIdentifier = @"CellIdentifier"; 
// Dequeue or create a cell of the appropriate type. 
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell= nil; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    [[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview]; 

    UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)]; 
    [selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"strip_s12A-1_h.png"]]]; 
    cell.selectedBackgroundView = selectview; 
    [selectview release]; 

    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)]; 
    //cellTitle.adjustsFontSizeToFitWidth=YES; 
    [cellTitle setBackgroundColor:[UIColor clearColor]]; 
    [cellTitle setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]]; 
    [cellTitle setTextColor:[UIColor blackColor]]; 
    cellTitle.tag = 100+indexPath.row; 

     cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:@"Des2"]; 

    [cell.contentView addSubview:cellTitle]; 

    [cellTitle release]; 
} 
return cell; 
    } 

里面写我想就足够了正确的唯一

+0

是否有可能在里面创建UITextView如果(cell == nil) – 2012-03-14 11:53:54

+1

检查上面的代码现在它的工作正常 – 2012-03-14 12:03:08

+0

这是适用于你还是需要.....? – 2012-03-14 12:09:14

3

的代码之间的差测试if (cell == nil)处理没有可重用细胞出队的情况,在这种情况下,您必须创建一个新细胞。当您创建新单元格时,您有责任构建其视图层次结构。

+0

如果你不介意可以详细说明一点点.. – 2012-03-14 11:51:23

+2

@MusthafaPP细胞回收,而不是实例化新的。原因:表现,速度。通常在[OOP](https://en.wikipedia.org/wiki/Object-oriented_programming)中,我们根据需要创建新的实例(对象)。但是实例化一个新对象有一些开销,需要一些时间。在极少数情况下,例如UITableView,我们需要最高的性能。回收现有对象,同时重新设置适合新鲜使用的关键属性/变量,可节省足够的时间以保证麻烦。所以,如果你有一个单元可用,请重新使用它。否则,创建一个新实例。 – 2013-12-03 03:06:13

相关问题