UITableView shows extra blank space on the left, after I inserted array of parsed data. So, my question is how to trim whitespace in the tableView. I guess I have the blank space because of href hyperlink to the text I parsed which is shown as blank in the cell.
This is the part of HTML I parsed:
<h3 style="clear:left"><a class="c4" href="http://www.website.ge/article-22624.html">
Facebook - the text I have parsed </a></h3>
This is the code I used:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSError *error = nil;
NSURL *url=[[NSURL alloc] initWithString:@"http://www.website.ge/news"];
NSString *strin=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
HTMLParser *parser = [[HTMLParser alloc] initWithString:strin error:&error];
if (error) {
NSLog(@"Error: %@", error);
return;
}
listData =[[NSMutableArray alloc] init];
HTMLNode *bodyNode = [parser body];
NSArray *dateNodes = [bodyNode findChildTags:@"span"];
for (HTMLNode *inputNode in dateNodes) {
if ([[inputNode getAttributeNamed:@"class"] isEqualToString:@"date"]) {
//NSLog(@"%@", [inputNode contents]);
//[listData addObject:[inputNode contents]];
}
}
NSArray *divNodes = [bodyNode findChildrenOfClass:@"c4"];
for (HTMLNode *inputNode in divNodes) {
[listData addObject:[inputNode contents]];
}
}
In the table view I see blank space at the beginning of the parsed data. It must be the hyperlink which is translated into blank space.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//here you check for PreCreated cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Fill the cells...
cell.textLabel.textAlignment=UITextAlignmentLeft; // attempt to move it to the left
cell.textLabel.text = [listData objectAtIndex: indexPath.row];
//yourMutableArray is Array
return cell;
}
I’m assuming, based on your description, that you have extra whitespace in the text which you are displaying in a label.
If this is the case, take the string that you have created, and before setting the label use this:
EDIT now that you have supplied your code:
In your case, I would use
stringByTrimmingCharactersInSetwhen you are first setting the strings in your array: