如何在 UITableViewCell 中添加多行文本字段?
How to add multiline text field in UITableViewCell?
我正在尝试开发在其单元格中包含多行文本字段的 UITableView。
我以编程方式创建了 UITableView 和单元格,此时我添加了一个普通的 UITextField,但我真的想要像 whats app 聊天输入字段这样的东西,所以当我输入更多字符时,它会自动增加它的高度和 UITableViewCell 高度。 (请检查附图)
我的 UITableView 委托方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [selectedTabFields count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell= [tableView1 dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[self getIntegerTextfield:indexPath.row]];
}
return cell;
}
//Integer Textfield
-(UITextField *)getIntegerTextfield:(NSUInteger)index{
UITextField *textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
textField.placeholder=@"Text here";
textField.tag=index;
textField.delegate=self;
textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
textField.textAlignment=UITextAlignmentLeft;
textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
textField.textColor=[UIColor darkGrayColor];
textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
return textField;
}
即使它尝试了此代码,但它不起作用,当我 运行 进行此更改时,UITableView 单元格为空。
Tried the same, but not working
请建议答案。
附加图片
既然有了第三方库,为什么还要重新发明轮子? This 一个完全符合您的要求。只需使用 UITextView
即可。
编辑: 既然你说上面的出于某种原因对你不起作用。您可以使用其他 SlackhQTextViewController 等
在getIntegerTextfield中添加:(NSUInteger)index{
textField.frame.size.height = (textField.text.length/a) * b;
a = 您在 xib 中添加的文本字段的大小。
b = 1 行文本字段的高度。
我正在尝试开发在其单元格中包含多行文本字段的 UITableView。 我以编程方式创建了 UITableView 和单元格,此时我添加了一个普通的 UITextField,但我真的想要像 whats app 聊天输入字段这样的东西,所以当我输入更多字符时,它会自动增加它的高度和 UITableViewCell 高度。 (请检查附图)
我的 UITableView 委托方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [selectedTabFields count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell= [tableView1 dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[self getIntegerTextfield:indexPath.row]];
}
return cell;
}
//Integer Textfield
-(UITextField *)getIntegerTextfield:(NSUInteger)index{
UITextField *textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
textField.placeholder=@"Text here";
textField.tag=index;
textField.delegate=self;
textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
textField.textAlignment=UITextAlignmentLeft;
textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
textField.textColor=[UIColor darkGrayColor];
textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
return textField;
}
即使它尝试了此代码,但它不起作用,当我 运行 进行此更改时,UITableView 单元格为空。 Tried the same, but not working 请建议答案。
附加图片
既然有了第三方库,为什么还要重新发明轮子? This 一个完全符合您的要求。只需使用 UITextView
即可。
编辑: 既然你说上面的出于某种原因对你不起作用。您可以使用其他 SlackhQTextViewController 等
在getIntegerTextfield中添加:(NSUInteger)index{
textField.frame.size.height = (textField.text.length/a) * b;
a = 您在 xib 中添加的文本字段的大小。 b = 1 行文本字段的高度。