将NSArray objectAtIndex显示到UILabel时出错

时间:2013-01-19 17:40:47

标签: objective-c object nsarray uilabel

尽管我已经做了很多次尝试..我无法正确地将NSArray中的项目显示为UILabel ..

当我NSLog数组时,控制台会将此返回给我:

2013-01-19 14:34:32.799 bloom[2766:c07] (
"0.877"
)

我从网站获取并解析的值等等。

问题在于,当我在UILabel中显示它时,它向我展示了完全相同的东西,我只需要显示没有引号的值。以下是它的显示方式:

Displaying problem

编辑:

这是我的代码:

- (void)configureView
{
// Update the user interface for the detail item.

if (self.detailItem) {

    for (NSDictionary *valuesDatum in _detailItem) {
        NSDictionary *itemAtIndex = (NSDictionary *)_detailItem;
        self.title = [itemAtIndex objectForKey:@"SYMBOL"];
        NSString *strUrl = @"http://www.bloomberg.com/quote/";
        NSString *ativo = [itemAtIndex objectForKey:@"SYMBOL"];
        NSString *consulta = [strUrl stringByAppendingString:ativo];

        NSURL *url = [NSURL URLWithString:consulta];

        NSData *webData = [NSData dataWithContentsOfURL:url];


        NSString *xPathQuery = @"//span[@class=' price'] | //span[@class=' trending_up  up'] | //span[@class=' trending_up up']/span | //table[@class='snapshot_table']/tr/td";

        TFHpple *parser = [TFHpple hppleWithData:webData isXML:NO];

        NSArray *array = [parser searchWithXPathQuery:xPathQuery];

        valores = [[NSMutableArray alloc]init];
        for (TFHppleElement *element in array) {
            [valores addObject:[[element firstChild] content]];
        }

        novosValores = [[NSMutableArray alloc]init];
        for (NSString *valuesDatum in valores) {
            NSString *removeNewLine = [[valuesDatum componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
            NSString *removeSpace = [removeNewLine stringByReplacingOccurrencesOfString:@"         " withString:@""];
            NSString *removeSpaceOne = [removeSpace  stringByReplacingOccurrencesOfString:@"    " withString:@""];
            NSString *trocaVirgulaPonto = [removeSpaceOne stringByReplacingOccurrencesOfString:@"," withString:@"."];
            [novosValores addObject:trocaVirgulaPonto];
        }

        valoresFinais = [[NSMutableArray alloc]init];
        for (NSString *valuesDatum in novosValores) {
            NSArray *val = [valuesDatum componentsSeparatedByString:@" - "];
            [valoresFinais addObject:val];
        }

        infos = [[NSMutableArray alloc]init];
        for (NSArray *dados in valoresFinais) {
            NSArray *arrayDados = [[NSArray alloc]initWithArray:dados];

            for (NSString *teste in arrayDados) {
                NSArray *arrayTeste = [teste componentsSeparatedByString:@","];
                [infos addObject:arrayTeste];
            }
        }
        NSLog(@"%@",[infos objectAtIndex:0]);

        NSString *fff = [[NSString alloc] initWithFormat:@"%@", [infos objectAtIndex:0]];
        [_detailDescriptionLabel setText:fff];
    }

    }
}

新编辑:

我有这个数组:

2013-01-19 15:43:05.055 bloom[3564:c07] (
"7.730",
"0.020",
"0.26%",
"7.750",
"7.650-7.800",
"2.333.100",
"7.710",
"3.730-8.810",
"+4.04%"

我需要的只是一个新数组,第5行和第8行的数据用“ - ”分隔。

所以任何人都有光?

感谢!!!

3 个答案:

答案 0 :(得分:1)

我在Anoop的帮助下解决了这个问题。我改变了一下他的代码,最后我的代码是这样的:

for (NSDictionary *valuesDatum in _detailItem) {
        NSDictionary *itemAtIndex = (NSDictionary *)_detailItem;
        self.title = [itemAtIndex objectForKey:@"SYMBOL"];
        NSString *strUrl = @"http://www.bloomberg.com/quote/";
        NSString *ativo = [itemAtIndex objectForKey:@"SYMBOL"];
        NSString *consulta = [strUrl stringByAppendingString:ativo];

        NSURL *url = [NSURL URLWithString:consulta];

        NSData *webData = [NSData dataWithContentsOfURL:url];

        NSString *xPathQuery = @"//span[@class=' price'] | //span[@class=' trending_up up'] | //span[@class=' trending_up up']/span | //table[@class='snapshot_table']/tr/td";

        TFHpple *parser = [TFHpple hppleWithData:webData isXML:NO];

        NSArray *array = [parser searchWithXPathQuery:xPathQuery];

        valores = [[NSMutableArray alloc]init];
        for (TFHppleElement *element in array) {
            [valores addObject:[[element firstChild] content]];
        }

        novosValores = [[NSMutableArray alloc]init];
        for (NSString *valuesDatum in valores) {
            NSString *removeNewLine = [[valuesDatum componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
            NSString *removeSpace = [removeNewLine stringByReplacingOccurrencesOfString:@"         " withString:@""];
            NSString *removeSpaceOne = [removeSpace stringByReplacingOccurrencesOfString:@"    " withString:@""];
            NSString *removeSpaceTwo = [removeSpaceOne stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSString *removeDash = [removeSpaceTwo stringByReplacingOccurrencesOfString:@" - " withString:@" "];
            NSString *trocaVirgulaPonto = [removeDash stringByReplacingOccurrencesOfString:@"," withString:@"."];

            [novosValores addObject:trocaVirgulaPonto];
        }

        NSString *fullString=[novosValores componentsJoinedByString:@"_"];
        NSString *changeDash = [fullString stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
        finalArray=[changeDash componentsSeparatedByString:@"_"];
        //NSLog(@"%@", finalArray);

    }
    NSString *str = [[NSString alloc]initWithFormat:@"%@",[finalArray objectAtIndex:10]];
    [_detailDescriptionLabel setText:str];

答案 1 :(得分:0)

 (
   "0.877"
)

以上是一个数组,您将该数组放在标签上。

您必须使用someLabel.text=[thatArray objectAtIndex:0];

编辑:

根据您的要求尝试以下方法:(未经过编译检查)

NSString *fullString=[array componentsJoinedByString:@"+"];
NSArray *brokenString=[fullString componentsSeparatedByString:@"-"];
NSString *mainString=brokenString[1];
NSArray *finalArray=[mainString componentsSepartedByString:@"+"];

答案 2 :(得分:0)

你确定你这样做了吗?

label.text = [array objectAtIndex:0];

而不是:

label.text = array;