在 NSDictionary 中插入 NSURL 或整数

发布于 2024-10-20 09:47:54 字数 2420 浏览 6 评论 0原文

我有一个类“book.h”,其中我声明了

Book.h

    NSInteger WeatherID;
    NSString *Description;  
    NSURL *PictureURL;  

BookDetailview.h

    #import <UIKit/UIKit.h>
    @class Book, XMLAppDelegate;
    @interface BookDetailViewController : UIViewController
{
    IBOutlet UITableView *tableView;
        XMLAppDelegate *appDelegate;
        Book *aBook;      //creating object of 'Book-class'
        NSMutableDictionary *EmpDictionary;
    NSMutableArray *EmpArray;
}
    @property (nonatomic, retain) Book *aBook;
    @property (nonatomic, retain) NSMutableArray *EmpArray;
    @end

BookDetailView.m

     (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"creation of cell");

    static NSString *CellIdentifier = @"Cell";
    ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // creating cell from ImageCell.m
    NSLog(@"calling image class");
    if (cell == nil) {
        cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; // allocating with frame
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSLog(@"abook ...pictutre url......%@" , aBook.PictureURL);
    EmpDictionary=[[NSMutableDictionary alloc]init];
    [EmpDictionary setValue:aBook.Description forKey:@"Description"];
    [EmpDictionary setValue:aBook.PictureURL forKey:@"PictureURL"];
    [EmpDictionary setValue:aBook.WeatherID forKey:@"WeatherID"];
    EmpArray=[[NSMutableArray alloc]init];
    [EmpArray addObject:EmpDictionary];
      NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
    [cell setData:itemAtIndex]; //call to 'setData' meyhod to ImageCell.m
    NSLog(@"set text of a cell"); 
    return cell;
}

ImageCell.m

    -(void)setData:(NSDictionary *)dic {
    self.titleLabel.text = [dic objectForKey:@"Description"];
    self.urlLabel.text = [dic objectForKey:@"PictureURL"];
    self.itemID = (NSInteger)[dic objectForKey:@"WeatherID"];
    NSLog(@"setting objects for keys");
    // setting up the imageView now
    self.imageView.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [dic objectForKey:@"PictureURL"]]]];
    NSLog(@"setting of image");
}

i have a class 'book.h' in which i have declared

Book.h

    NSInteger WeatherID;
    NSString *Description;  
    NSURL *PictureURL;  

BookDetailview.h

    #import <UIKit/UIKit.h>
    @class Book, XMLAppDelegate;
    @interface BookDetailViewController : UIViewController
{
    IBOutlet UITableView *tableView;
        XMLAppDelegate *appDelegate;
        Book *aBook;      //creating object of 'Book-class'
        NSMutableDictionary *EmpDictionary;
    NSMutableArray *EmpArray;
}
    @property (nonatomic, retain) Book *aBook;
    @property (nonatomic, retain) NSMutableArray *EmpArray;
    @end

BookDetailView.m

     (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"creation of cell");

    static NSString *CellIdentifier = @"Cell";
    ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // creating cell from ImageCell.m
    NSLog(@"calling image class");
    if (cell == nil) {
        cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; // allocating with frame
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSLog(@"abook ...pictutre url......%@" , aBook.PictureURL);
    EmpDictionary=[[NSMutableDictionary alloc]init];
    [EmpDictionary setValue:aBook.Description forKey:@"Description"];
    [EmpDictionary setValue:aBook.PictureURL forKey:@"PictureURL"];
    [EmpDictionary setValue:aBook.WeatherID forKey:@"WeatherID"];
    EmpArray=[[NSMutableArray alloc]init];
    [EmpArray addObject:EmpDictionary];
      NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
    [cell setData:itemAtIndex]; //call to 'setData' meyhod to ImageCell.m
    NSLog(@"set text of a cell"); 
    return cell;
}

ImageCell.m

    -(void)setData:(NSDictionary *)dic {
    self.titleLabel.text = [dic objectForKey:@"Description"];
    self.urlLabel.text = [dic objectForKey:@"PictureURL"];
    self.itemID = (NSInteger)[dic objectForKey:@"WeatherID"];
    NSLog(@"setting objects for keys");
    // setting up the imageView now
    self.imageView.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [dic objectForKey:@"PictureURL"]]]];
    NSLog(@"setting of image");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-10-27 09:47:54
[dict setValue:value forKey:key];

[dict setObject:object forKey:key];
[dict setValue:value forKey:key];

[dict setObject:object forKey:key];
妄司 2024-10-27 09:47:54
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                       @"val1", @"key1", @"val2", @"key2", @"val3", @"key3",@"val4", @"key4",@"val5", @"key5",nil];
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                       @"val1", @"key1", @"val2", @"key2", @"val3", @"key3",@"val4", @"key4",@"val5", @"key5",nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文