滚动表时,将刷新TextField

时间:2011-11-08 11:05:09

标签: iphone ios cocoa-touch uitableview uitextfield

请参阅代码

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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
UILabel *playerHeading;
UITextField *txt;


if (cell == nil)
{
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:SimpleTableIdentifier] autorelease];
    playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)];
    txt=[[UITextField alloc]initWithFrame:CGRectMake(80.0f, 13.0f, 170.0f, 30.0f)];
    txt.tag=2;
    [txt setBackgroundColor:[UIColor clearColor]];

    //[txt setTextColor:[UIColor blackColor]];

    //[txt setBackgroundColor:[UIColor whiteColor]];
    [txt setTextColor:[UIColor grayColor]];
    txt.delegate=self;
    //[txt setBorderStyle:UIBorderStyle ]
    [txt setText:@"Enter Player name"];
    //txt.layer.cornerRadius=8.0f;
    //   txt.layer.masksToBounds=YES;
    //txt.layer.borderColor=[[UIColor redColor]CGColor];
    //   txt.layer.borderWidth= 1.0f;


    [playerHeading setBackgroundColor:[UIColor clearColor]];
    [playerHeading setFont:[UIFont boldSystemFontOfSize:15]];
    [playerHeading setTag:1];
    [playerHeading setBackgroundColor:[UIColor clearColor]];


    NSString *str=[NSString stringWithFormat:@"Player %i",indexPath.row];
    [playerHeading setText:str];
    [playerHeading setFont:[UIFont boldSystemFontOfSize:15]];

//          NSLog(@"row %i ,section %i",indexPath.row,indexPath.section);


    [cell.contentView addSubview:playerHeading];
}
else
{
    playerHeading = [cell.contentView viewWithTag:1];
    txt=[cell.contentView viewWithTag:2];

//  NSLog(@"row %i ,section %i",indexPath.row,indexPath.section);


}
if(indexPath.section%2==0){

    playerHeading.textColor=[UIColor blueColor];

}

else{
    playerHeading.textColor=[UIColor redColor];

}

playerHeading.text = [NSString stringWithFormat:@"Player %i",indexPath.row];

    txt.tag=indexPath.section;
   //   NSString *nam=[NSString stringWithFormat:@"%i",indexPath.row];
   //   [txt setName:nam];

//txt.clearsOnBeginEditing=YES;
[cell.contentView addSubview:txt];
//cell.accessoryView=txt;
return cell;

   }

我创建了一个带有7个部分的UITable,每个部分包含4个玩家用户需要填写到达行中的文本字段,但问题是当我滚动表格时,textfields设置为默认值请帮助我如何设置文本字段到使用

插入的固定值

1 个答案:

答案 0 :(得分:0)

要解决您的问题,请将用户插入的字符串存储到数据结构中,以便在部分的帮助下轻松访问字符串。

你可以使用 NSMutableDictionary ,其键将是节号,它将存储一个数组,'row'数组索引将给出你在那个特定的文本字段中键入的字符串。

cellForRowAtIndexPath 中,您必须检查该单元格是否存在字符串,否则显示默认字符串或占位符。