简体   繁体   中英

xcode - How to load images from xml the right way?

I'm quite new to xcode na objective-c so I'm sorry for the dumb question. What I want to do is to load images to cells in UITableView according to id. Every object in xml has the id and the image name id id.jpg. I accomplished to load them right but when I scroll the images dissapear. From what I have found it looks like it has something to do with dequeueReusableCellWithIdentifier but I can't figure out what is it. Here is the code I'm using to load the images:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    theList = [app.listArray objectAtIndex:indexPath.row];

    NSString *trimmedString = [theList.t_image stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    cell.imageView.image = [UIImage imageNamed:trimmedString];
    cell.textLabel.text = theList.t_name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    if(cell.imageView.image == nil){
        cell.imageView.image = [UIImage imageNamed:@"untitled.jpg"];
    }

    return cell;
}

and here is the xml:

<taxis>
<taxi>
<t_id>1</t_id>
<t_image>1.jpg</t_image>
<t_name>AAA Taxi</t_name>
<t_tel>+42014014</t_tel>
<t_addr>Vodičkova 40</t_addr>
<t_web>www.radiotaxiaaa.cz</t_web>
<t_getin>40</t_getin>
<t_km>26.9</t_km>
<t_wait>5</t_wait>
<t_city>Praha</t_city>
</taxi>
<taxi>
<t_id>2</t_id>
<t_image>2.jpg</t_image>
<t_name>Modrý anděl</t_name>
<t_tel>+420737222333</t_tel>
<t_addr>Cukrovarská 33</t_addr>
<t_web>www.modryandel.cz</t_web>
<t_getin>40</t_getin>
<t_km>19</t_km>
<t_wait>6</t_wait>
<t_city>Praha</t_city>
</taxi>
</taxis>

Thanks for any answer.

do all rows have picture? if not make sure to set cell.imageView to nil at the beginnign of the method after

if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

UPDATE : If i were you i would cache the images in an array and load them from this array in cellforrowatindexpath method.Try this i will probably work.Note that you can not add nil as object to NSmutablearray so you need to add another value rather than nil and UIimage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM