2012-05-30 97 views
-1

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

这是我解析的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; 
} 

回答

1

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

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

现在你已经提供的代码
theStringToDisplay = [theStringToDisplay stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
theLabel.text  = theStringToDisplay; 

编辑:
在你的情况,当您第一次设置阵列中的字符串时,我会使用stringByTrimmingCharactersInSet

NSArray *divNodes = [bodyNode findChildrenOfClass:@"c4"]; 
for (HTMLNode *inputNode in divNodes) { 
    [listData addObject:[[inputNode contents] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 
} 
+0

我应该如何将此方法应用于数组? – Benjamen

+0

它看起来我需要但它说whitespaceCharacterSet是未声明的标识符。 – Benjamen

+0

这是一个类方法,所以你需要[NSCharacterSet whitespaceCharacterSet] – rdelmar