首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView重装

UITableView重装
EN

Stack Overflow用户
提问于 2011-12-16 05:28:12
回答 1查看 266关注 0票数 0

我在重新装填桌子时遇到了一些问题。我的cellForRowAtIndexPath是:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    int counter=indexPath.row;

    NSString *CellIdentifier = [NSString stringWithFormat:@"%d",counter];

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;


    //ID
    UILabel *lblID=[[UILabel alloc]init];
    lblID.frame=CGRectMake(10, 15.0, 150, 30.0);
    [lblID setFont:[UIFont boldSystemFontOfSize:20.0]];
    [lblID setBackgroundColor:[UIColor clearColor]];
    [lblID setTextColor:[UIColor blackColor]];
    lblID.text = [arrID objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lblID];
    [lblID release];

    //Date
    UILabel *lblName=[[UILabel alloc]init];
    lblName.frame=CGRectMake(130, 15.0, 250, 30.0);
    [lblName setFont:[UIFont systemFontOfSize:20.0]];
    [lblName setBackgroundColor:[UIColor clearColor]];
    [lblName setTextColor:[UIColor blackColor]];
    lblName.text = [arrProductName objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lblName];
    [lblName release];

    //Qty
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(320, 20, 50, 30)];
    [textField addTarget:self action:@selector(TextOfTextField:)
        forControlEvents:UIControlEventEditingDidEnd];
    textField.userInteractionEnabled = true;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font = [UIFont systemFontOfSize:15];
    textField.text = [arrItems objectAtIndex:indexPath.row];
    textField.tag = indexPath.row;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypePhonePad;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
    textField.delegate = self;
    [arrTxtItems addObject:textField];
    [cell.contentView addSubview:textField];
    [textField release];

    //Discount
    UITextField *txtDiscount = [[UITextField alloc] initWithFrame:CGRectMake(460, 20, 50, 30)];
  //  [txtDiscount addTarget:self action:@selector(TextOfTextField:)
    //    forControlEvents:UIControlEventEditingDidEnd];
    txtDiscount.userInteractionEnabled = true;
    txtDiscount.borderStyle = UITextBorderStyleRoundedRect;
    txtDiscount.font = [UIFont systemFontOfSize:15];
    txtDiscount.text = [arrDiscount objectAtIndex:indexPath.row];
    txtDiscount.tag = indexPath.row;
    txtDiscount.autocorrectionType = UITextAutocorrectionTypeNo;
    txtDiscount.keyboardType = UIKeyboardTypePhonePad;
    txtDiscount.returnKeyType = UIReturnKeyDone;
    txtDiscount.clearButtonMode = UITextFieldViewModeWhileEditing;
    txtDiscount.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
    txtDiscount.delegate = self;
    [arrTxtDiscount addObject:txtDiscount];
    [cell.contentView addSubview:txtDiscount];
    [txtDiscount release];

    //Price
    UILabel *lblPrice=[[UILabel alloc]init];
    lblPrice.frame=CGRectMake(560, 15.0, 250, 30.0);
    [lblPrice setFont:[UIFont boldSystemFontOfSize:20.0]];
    [lblPrice setBackgroundColor:[UIColor clearColor]];
    [lblPrice setTextColor:[UIColor blackColor]];
    [arrLblNetPrice addObject:lblPrice];
    NSString *strPrice = [arrPrice objectAtIndex:indexPath.row];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    [formatter setGroupingSeparator:@"."];
    [formatter setGroupingSize:2];
    [formatter setUsesGroupingSeparator:YES];
    [formatter setSecondaryGroupingSize:3];
    NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[strPrice doubleValue]]];
    [formatter release];
    NSLog(@"str :   %@",str);
    NSLog(@"strPrice :    %@",strPrice);
    lblPrice.text = str;
    [cell.contentView addSubview:lblPrice];
    [lblPrice release];
    }
        return cell;
}  

以这种方式构造单元格可以帮助我在滚动表时保留textFields的值。但是在某种程度上,当我重新加载表控件时,如果条件为,则表控件不会掉到中,因为单元格还没有释放。在这种情况下,我应该把牢房放哪儿?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-16 05:38:01

您需要将创建/获取单元格实例与使用数据配置单元格属性分开。更改代码如下:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    int counter=indexPath.row;

    NSString *CellIdentifier = [NSString stringWithFormat:@"%d",counter];

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        //ID
        UILabel *lblID=[[UILabel alloc]init];
        lblID.frame=CGRectMake(10, 15.0, 150, 30.0);
        [lblID setFont:[UIFont boldSystemFontOfSize:20.0]];
        [lblID setBackgroundColor:[UIColor clearColor]];
        [lblID setTextColor:[UIColor blackColor]];
        lblID.tag = MyViewTagIDLabel;      // make an enum to give your subviews unique tags > 0
        [cell.contentView addSubview:lblID];
        [lblID release];

        // add the rest of your subviews

        // any other cell configuration that does not change based on the ind
    }

    // configure the cell with data based on the indexPath
    UILabel lblID = [cell.contentView viewWithTag:MyViewTagIDLabel];
    lblID.text = [arrID objectAtIndex:indexPath.row];

    // configure the rest of the subviews

    return cell;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8530174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档