ios - 粘贴文本会产生额外的空间

标签 ios objective-c

在我的应用程序中,我需要将文本粘贴到 UITextView 中并在 UITableView 中逐行查看它,但是当我粘贴时遇到了一个不寻常的问题在应用程序内的文本,它会创建一个额外的未使用行,并将文本移动到它下面的行。 For example .在图像中,您可以看到我在 TextView 的顶部输入文本,每次向下移动时都会创建一个空行。

这是我的代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


cell.textLabel.text = [NSString stringWithFormat:@"%@",[listArray objectAtIndex:indexPath.row]];;


return cell;
}
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


 return [listArray count];

}



-(void)textViewDidChange:(UITextView *)textView{

listArray=[[NSMutableArray alloc]initWithArray:[textView.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

 [self.tableView reloadData];
}

我粘贴的所有内容都不会发生这种情况,但基本上我需要输入到应用程序中的一段主要文本确实会发生这种情况。

最佳答案

所以,您可以通过以下方式删除多余的空间

NSString *yourText = @" remove extra space  ";
NSString *finalText = [yourText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

关于ios - 粘贴文本会产生额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151635/

相关文章:

ios - UIPickerView.selectRow 滚动导致崩溃

ios - AF 网络 2.0 : Passing header information

ios - 合并两个 NSRegularExpression

ios - 使 UITableView 行可选择

ios - NSCounted 设置忽略属性

iphone - 如何使用 Mobile Safari 中的链接使用 Twitter 应用程序打开用户的 Twitter 个人资料?

ios - 快速 3D Peek 和 POP

ios - 是否可以让airdrop同时向多个设备发送文件?

ios - Mute Video + Transform Video 使用 AVAssetExportSession 的单一导出操作

iphone - 我如何检测cocos2d中的触摸?