I am trying to implement a UITableView based application. To do this, I select UITableViewStyle - Group.In my TableView is section 15 , each section has 1 row.
- (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; } }
I want to add a UITextView to section 12
For this, I made the following code
- (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; }
At the initil stage, a table view like this
if I scroll the table up and down, then the uitextView section has changed and will display several places.
I canβt understand my mistake, why did this happen?
if I implement the above code as 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) { **** [cell.contentView addSubview:descriptionTextField]; [descriptionTextField release]; } } return cell;
}
UITextView is not suitable, I think it does not allocate.
so what is the difference between embedding code in if (cell == nil) {inside}
if (cell == nil) {} side