-1

在我插入解析数据数组后,UITableView 在左侧显示了额外的空白区域。所以,我的问题是如何修剪 tableView 中的空白。我想我有空格,因为我解析的文本的 href 超链接在单元格中显示为空白。

这是我解析的 HTML 的一部分:

 <h3 style="clear:left"><a class="c4" href="http://www.website.ge/article-22624.html">
         Facebook - the text I have parsed </a></h3>

这是我使用的代码:

- (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]];

            }

}

在表格视图中,我在解析数据的开头看到了空格。它必须是被翻译成空格的超链接。

- (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;
}
4

1 回答 1

1

根据您的描述,我假设您在标签中显示的文本中有额外的空格。

如果是这种情况,请使用您创建的字符串,并在设置标签之前使用:

theStringToDisplay = [theStringToDisplay stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
theLabel.text      = theStringToDisplay;

EDIT now that you have supplied your code:
In your case, I would use stringByTrimmingCharactersInSet when you are first setting the strings in your array:

NSArray *divNodes = [bodyNode findChildrenOfClass:@"c4"];
for (HTMLNode *inputNode in divNodes) {
    [listData addObject:[[inputNode contents] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
} 
于 2012-05-30T03:37:54.380 回答