【问题标题】:How to populate tableview controller with text input如何使用文本输入填充 tableview 控制器
【发布时间】:2012-11-21 12:55:26
【问题描述】:

我创建了一个带有UITextField 的表视图控制器。如何填充来自UITextField 的输入文本的表格视图控制器单元格。我尝试了以下但似乎数组为空或被清空

- (IBAction)endingInput:(id)sender{
    [sender resignFirstResponder];

    //send the message to the table cell
    NSMutableArray *history = [[NSMutableArray alloc] init];
    [history addObject:[NSString stringWithFormat:@"%@",self.chatMessageField.text]];
    [self.chatHistory addObject:history];
    [myTableView reloadData];

    //push the message to server
    [self initiateMessageSending:self.chatMessageField.text];

    chatMessageField.text = @"";
    [history release];
}

关于我的cellForRowAtIndex 方法;

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if(tableView == self.myTableView){
    cell.textLabel.text = [chatHistory objectAtIndex:indexPath.row];
}
return cell;

我已经设置了一个断点,但它没有通过那里。我怀疑我的数组是空的。

同样,调用reloadData 方法的最佳位置在哪里?

另外,chatHistory 是私有成员,有没有办法像[[self.chatHistory alloc] init]; 一样初始化它?

我知道这是一个常见问题,但我一直在讨价还价。

【问题讨论】:

  • 你初始化 chatHistory 了吗?
  • @PratyushaTerli - 现在我做到了,但仍然无济于事,我有 NSLog 我的数组,它显示它有我制作的编码文本输入,我不能做的是让内容要显示到 tableview 单元格的数组
  • 注释掉if(tableView == self.myTableView){}并尝试
  • 我认为这里的每个人的答案都添加到了解决问题的检查中,但有趣的是,罪魁祸首是表没有委托和数据源连接......所以总是在检查连接之前做任何其他事情。对于给您带来的麻烦,我深表歉意,并感谢您的及时回复。

标签: ios arrays controller nsmutablearray tableview


【解决方案1】:

我想你忘记分配和初始化你的chatHistory

- (void)viewDidLoad
{
    self.chatHistory = [[NSMutableArray alloc] init];
}

【讨论】:

  • 不客气!如果你已经解决了你的问题他/她的回答,你应该接受某人的回答。
【解决方案2】:

如果你没有初始化chatHistory,它甚至都不存在......别介意是空的! (我不清楚这里的“私人会员”是什么意思。)

如果您记录 chatHistory 的值,您将确定它是 nil 还是空。

我看到的另一个问题是您尝试将 chatHistory 与其他 (history) 数组一起加载,然后将其内容用作字符串。您应该决定它是数组数组还是字符串数组,然后保持一致。

(另外[[self.chatHistory alloc] init]; 不是创建新对象的有效语法。请查看Objective-C language reference 以了解正确的方法。)

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多