为什么我们在UITableViewController中检查(cell == nil)?
我想实现基于UITableView的Application.For我selectUITableViewStyle是Group.In我的TableView他们是15节每节有1行。
- (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; } }
我想在第12节添加一个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阶段,表格视图就像这样
如果我滚动tableview上下,然后uitextView部分改变,它会显示多个位置。
我不明白我的错,为什么发生这种事?
如果在上面的代码中实现if(cell == nil)
- (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,我认为它不是分配。
那么在if(cell == nil){inside}中实现的代码有什么区别?
如果(cell == nil){}在外面
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; }
我认为就够了
testingif (cell == nil)
处理没有可重用的单元出队的情况,在这种情况下,您必须创build一个新的单元。 当您创build新的单元格时,您有责任构build其视图层次结构。