在我的应用程序中,我需要将文本粘贴到 UITextView
中并在 UITableView
中逐行查看它,但是当我粘贴时遇到了一个不寻常的问题在应用程序内的文本,它会创建一个额外的未使用行,并将文本移动到它下面的行。 .在图像中,您可以看到我在 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/