0
static NSString *cellIdentifier = @"Cell";


NSMutableArray *fields=[[NSMutableArray alloc] init];

MDSpreadViewCell *cell = [aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[[MDSpreadViewCell alloc] initWithStyle:MDSpreadViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

        for (i =0; i < N; i++){
        NSLog (@"tag = %i", i);

        [fields  addObject:[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)]];
        [[fields objectAtIndex:i] setTag:i];

        NSString *John =  [NSString stringWithFormat:@"%i", i];

             [[fields objectAtIndex:i] setText:John];

      [[fields objectAtIndex:i] setBackgroundColor:[UIColor redColor]];
        [[fields objectAtIndex:i] setDelegate:self];
        [[fields objectAtIndex:i] setUserInteractionEnabled:TRUE];
           viewWithTag:i]).text;
            [cell addSubview:[fields objectAtIndex:i]];
    }

    return cell;

输出到控制台的内容是正确的:Tag = 0、1、2、3 等,但每个单元格只表示数组中的最后一个值。我尝试将 for 语句放在不同的位置,并在不同的块中返回单元格。我确信这是我忽略的简单事情。

4

1 回答 1

0

这可能只是一个部分有用的答案,因为您还有一些其他问题需要考虑,到目前为止我在您显示的代码片段中没有看到这些问题(最值得注意的是,您需要某种模型,例如一个多维数组,它独立于表格视图保存电子表格数据,以及当用户更改文本字段中的任何值时将更新模型的代码。)

但我认为此时您只是尝试在表格单元格中创建文本字段并让一些数字出现在其中。你可以尝试这样的事情:

static NSString *cellIdentifier = @"Cell";
MDSpreadViewCell *cell = [aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[[MDSpreadViewCell alloc] initWithStyle:MDSpreadViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)]];

    textField.tag = 1;
    textField.backgroundColor = [UIColor redColor];
    textField.delegate = self;
    [textField setUserInteractionEnabled:YES];

    [cell addSubview:textField];
}

UITextField *textFieldForCell = [cell viewWithTag:1];
int numberInCell = rowPath.row + columPath.column;
textFieldForCell.text = [NSString stringWithFormat:@"%i", numberInCell];

return cell;

就像将来的注释一样,在您原来的问题中表明您正在使用MDSpreadView会很有帮助

于 2012-08-13T06:38:13.823 回答