简体   繁体   中英

When we scroll custom table then cell content change in iPhone

hello friends I have created custom table and add all needed delegate and datasource for table then this is working good but when we scroll my table then content change like this is code...

Table_worklist=[[UITableView alloc]initWithFrame:CGRectMake(10,480,750,130)style:UITableViewStylePlain];
Table_worklist.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Table_worklist.delegate=self;
Table_worklist.dataSource=self;
Table_worklist.layer.borderWidth = 2.0;
Table_worklist.layer.borderColor = [UIColor grayColor].CGColor;
[ScrollView addSubview:Table_worklist];

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell== nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

     UITextField  *resultval=[[UITextField alloc]initWithFrame:CGRectMake(510,10,120,30)];
        resultval.tag=1;
        resultval.font = [UIFont boldSystemFontOfSize:12.0];
        //the horizontal alignment of the text
        resultval.textAlignment = NSTextAlignmentCenter;
    resultval.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
        resultval.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
        resultval.delegate =self;
    if([[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Numerical"])
  {
    imageLayer = field.layer;
    [imageLayer setCornerRadius:02];
    [imageLayer setBorderWidth:1];
    imageLayer.borderColor=[[UIColor lightGrayColor]CGColor];

   }
    else if([[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Words"]||[[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Comment"])
{
    imageLayer = field.layer;
    [imageLayer setCornerRadius:06];
    [imageLayer setBorderWidth:1];
    imageLayer.borderColor=[[UIColor blackColor]CGColor];
   UIButton *CheckMark=[[UIButton alloc]initWithFrame:CGRectMake(540,10,30,30)];
    CheckMark.tag=1;
    [CheckMark setImage:[UIImage imageNamed:@"DownTriangle1"]forState:UIControlStateNormal];

    imageLayer = CheckMark.layer;
    [imageLayer setCornerRadius:02];
    [imageLayer setBorderWidth:1];
    imageLayer.borderColor=[[UIColor blackColor] CGColor];
    [cell.contentView addSubview:CheckMark];

  }

        [cell.contentView addSubview:resultval];

  }

   return cell;
 }

this show right data for first 3or4 cell(because table height 130 so show only 3 cell at a time )for these 3 cell my table show right data ...for example

   if([[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Numerical"])
   {     }

then data is numerical go this if condition other wise go else condition...... but when we have for more cell like as 7-8 cell then when we scroll these cell so what happened suppose at cell-4 my controller go else if condition forComment and word data .........so at last cell -8 got numerical data so controller go in if condition but check mark button which was created in if else condition which show on my if condition data ......so i don't know what happened when we scroll how to change my condition ...... Actually i thing when last created else if condition (which create uibutton checkmark)on cell-4 which are apply on cell-8...........but controller go in right place in if condition at cell-8 .....but last time created checkmark button generated on cell-8 ...... how to solve this problem....

Before You follow my Answer i want to tell you that following code is bad for memory management because it will create new cell for each rows of UITableView , so be careful for it.

But it is better to use, When UITableView Have Limited rows (about 50-100 may be ) then following code is helpful in your case, use it if is it suitable for you.

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         /// Put your code here
     }

    return cell;
}

If you have limited rows then this is best code for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM