使用UITextView的标记属性将UITextView添加到UITableViewCell时出现问题

使用UITextView的标记属性将UITextView添加到UITableViewCell时出现问题,uitableview,Uitableview,我有一个分组表,其中每一行都是它自己的部分。我正在尝试将其中两行的UITextView作为UITableViewCell的contentView的子视图。这是我的cellForRowAtIndexPath方法(相关部分): 我知道我的部分是正确的,因为在我的UI中,我看到了相应的名称和注释。然而,我的UITextView最终都是橙色的。(我不想要颜色,但我正在尝试解决为什么在textView委托方法中获取错误值的问题) 在我的常量文件中,我将标记声明为: const NSInteger kNam

我有一个分组表,其中每一行都是它自己的部分。我正在尝试将其中两行的UITextView作为UITableViewCell的contentView的子视图。这是我的cellForRowAtIndexPath方法(相关部分):

我知道我的部分是正确的,因为在我的UI中,我看到了相应的名称和注释。然而,我的UITextView最终都是橙色的。(我不想要颜色,但我正在尝试解决为什么在textView委托方法中获取错误值的问题)

在我的常量文件中,我将标记声明为:

const NSInteger kNameTextViewTag = 10;
const NSInteger kNotesTextViewTag = 20;
出于某种原因,我的tableViewCell最终使用了错误的文本视图。这有什么原因吗?我假设它与dequeuReusableCellWithIdentifier方法有关,但不确定。谢谢

已编辑 根据Martin的评论,我尝试了这个,但仍然不起作用:

 static NSString *NameTextViewCellIdentifier = @"NameTextViewCellIdentifier";
    static NSString *NotesTextViewCellIdentifier = @"NotesTextViewCellIdentifier";


else if (section == kNameSection) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NameTextViewCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:LeftDetailCellIdentifier];
        }

        cell.textLabel.text = @"name";

        UITextView *nameTextView = [[UITextView alloc] initWithFrame:CGRectMake(80, 12, kTextViewWidth, kTextViewHeight)];
        nameTextView.text = self.address.name;

        nameTextView.font = [UIFont boldSystemFontOfSize:12.0];
        nameTextView.tag = kNameTextViewTag;
        nameTextView.backgroundColor = [UIColor blueColor];
        nameTextView.delegate = self;

        [cell.contentView addSubview:nameTextView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    else if (section == kNotesSection) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NotesTextViewCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:LeftDetailCellIdentifier];
        }

        cell.textLabel.text = @"notes";

        UITextView *notesTextView = [[UITextView alloc] initWithFrame:CGRectMake(80, 12, kTextViewWidth, kTextViewHeight)];
        notesTextView.text = self.address.notes;

        notesTextView.font = [UIFont boldSystemFontOfSize:12.0];
        notesTextView.tag = kNotesTextViewTag;
        notesTextView.backgroundColor = [UIColor orangeColor];
        notesTextView.delegate = self;

        [cell.contentView addSubview:notesTextView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
我在这个类的父类中有UITextViewDelegate,因为我有另一个类也使用代码按需调整UITextView的大小

- (void)textViewDidChange:(UITextView *)textView {
    // Calculate the size of the text to reload the height for that table row
    NSString *tempString = textView.text;
    NSString *trimmedString = [tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"tag: %i ", textView.tag);
    if (textView.tag = kNameTextViewTag) {
        self.address.name = trimmedString;
    }
    else {
        self.address.notes = trimmedString;
    }

    [self setTextViewSize:textView]; // set proper text view size

    UIView *contentView = textView.superview;
    // (1) the padding above and below the UITextView should each be 6px, so UITextView's
    // height + 12 should equal the height of the UITableViewCell
    // (2) if they are not equal, then update the height of the UITableViewCell
    if ((textView.frame.size.height + 12.0f) != contentView.frame.size.height) {
        [self.tableView beginUpdates];
        [self.tableView endUpdates];

        [contentView setFrame:CGRectMake(0,
                                         0,
                                         contentView.frame.size.width,
                                         (textView.frame.size.height+12.0f))];
    }
}

所以我的标签还是不正确的。当通过名称textView调用此委托方法时,将记录正确的标记索引。但是,当我更新notes textview时,它会为一个键条目记录正确的textview,然后再次开始记录名称textview。不知道为什么。

您应该为每种单元格类型使用不同的重用标识符,否则可能会发生这样的情况,即您为“notes部分”构建的单元格稍后会被重用为“name部分”,反之亦然。

在创建了一个测试项目之后,我重新查找代码,看看它在哪里被破坏。在我的textViewDidChange方法中,我将noob错误设置为=而不是==。抱歉给你添麻烦了

if (textView.tag = kNameTextViewTag) {
        self.address.name = trimmedString;
    }
    else {
        self.address.notes = trimmedString;
    }

我刚才试过了。我添加了我在上面尝试过的代码。它仍然不起作用。我的背景色看起来是正确的,但是当我开始在tableViewCell中编辑适当的textView时,我会记录UITextView标记,对于第二个单元格(备注),它会首先记录正确的标记,然后开始记录第一个单元格(名称)。还不知道为什么。很好,它现在起作用了,但这似乎和你的第一个问题不同。如果需要使用不同的重用标识符来解决原始问题,那么您应该“接受”我的答案。否则,您应该“接受”您自己的答案,以便将此问题从“未回答问题”列表中删除。
if (textView.tag = kNameTextViewTag) {
        self.address.name = trimmedString;
    }
    else {
        self.address.notes = trimmedString;
    }