如何在表格视图的顶部显示新图像

时间:2011-04-22 18:04:41

标签: iphone

我通过传递xml参数从.net Web服务器获取图像。

我得到了更多的20张图片,我正在逐一将图像保存在iphone模拟器文档中。

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
        if( [elementName isEqualToString:@"photo"])
        {
            recordResults_photo = FALSE;
            NSData *decode = [Base64 decode:resultData_photo];
            NSString *theXML = [[NSString alloc] initWithData:decode encoding:NSUTF8StringEncoding];
                NSData *decodedimage = [Base64 decode:theXML];
                UIImage *image = [UIImage imageWithData:decodedimage];
                NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                NSLog(@"saving msg image %d",i);
                NSString *pngFilePath = [NSString stringWithFormat:@"%@/image%d.png",docDir,i];
                NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
                [data1 writeToFile:pngFilePath atomically:YES];

    }
}

然后图像像这个image0.png,image1.png,image2.png .....

一样保存

我正在这样的表视图中显示这些图像。

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSString *pngFilePath = [NSString stringWithFormat:@"%@/image%d.png",docDir,indexPath.row];

        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(250, 05, 30, 30)];
        img.image = [UIImage imageWithContentsOfFile:pngFilePath];
        [cell.contentView addSubview:img];

然后图像显示良好。

但现在我有一个问题。

我从网络服务器获取更多新图像,我需要在表格视图的顶部显示它们。

根据我的实现,我需要更改所有图像名称。

这不是正确的方式,是否有任何替代方式或更好的方式。

任何人都可以帮助我。

提前感谢你

1 个答案:

答案 0 :(得分:0)

看到这个答案:

how to add new elements in array at the top of the table view

这比我对同一个问题的回答要好得多,但两者都有效。

我刚刚确认这确实会将对象向下移动一个,所以这应该可行。

编辑:对不起,我误解了你的问题。至于更改图像名称,我认为可能的唯一方法是迭代数组并在添加新对象后更改名称。