data in the table row , appears and disappears when we scroll up (or) scroll down the page
I have created a grouped table view like in the image below. but data in the table row , appears and disappears when we scroll up (or) scroll down the page , when we scroll up first 2 rows will go up (beyond the view) then if we scroll down both rows will take same value,, y its so? can any one help me
Thanks in advance.
like following code i am designing the table cell where CustomCellStudentData
is class there i am creating like lable frames, text field frames
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[Cus开发者_开发知识库tomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(100,10, 200, 40)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].placeholder =@"<Student Name>";
cell.primaryLabel.text =@"Student Name: ";
}
break;
case 1:
{
tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(100,10, 200, 40)];
cell.accessoryView = tfText[1];
tfText[1].delegate=self;
tfText[1].placeholder =@"<Student Age>";
cell.primaryLabel.text =@"Student Age: ";
}
}
}
UITableView
reuses the cells so as to not bog down the memory. As soon as the two cells go up, they enter the pool of reusable cells. If the pool is not empty, dequeueReusableCellWithIdentifier:
returns a cell from that pool (there is a dependency on the identifier here). As the table view needs cells for the new rows being scrolled in, it gets the cells from the pool and provides it to you for reconfiguration. So, the text field that you attached to the cell when configuring it for row 0 and 1 still remain unless you explicitly remove. Since we don't which cells we are going to get, we should reset all customizations that we do and configure it as a new cell. In this case, irrespective of the cell coming in, you should remove the text field as the accessory view first.
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
What Deepak said is correct. Adding to his answer, in future, if you add any subView to cell view. Remove from the cell before you add anything on it. Use
for(UIView * view in cell.contentView.subviews){
[view removeFromSuperview]; view = nil;
}
before customizing the cell.
And yes, please increase your acceptance rate. Going back to the questions you asked before and accepting some answers should help.
精彩评论