Reputation: 221
In my application i need to paste text into a UITextView
and view it row by row in a UITableView
, however i have come across an unusual problem where when i paste text inside the application it creates an extra unused row and moves to the text to the row below it. . In the image you can see that i enter the text at the top in a TextView and it creates an empty row each time it moves down.
Heres my code.
-(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];
}
This doesn't happen for everything i paste but does happen for basically the one main piece of text i need to enter into the application.
Upvotes: 0
Views: 987
Reputation: 2999
so, you can remove extra space by the following way
NSString *yourText = @" remove extra space ";
NSString *finalText = [yourText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Upvotes: 2