开发者

Issue with custom TableViewCells in table display from NSMutableArray

开发者 https://www.devze.com 2023-02-19 13:40 出处:网络
I\'m having a weird issue - in my code I am fetching data from the internet in XML format and fill a NSMutableArray (altogether 34 items).

I'm having a weird issue - in my code I am fetching data from the internet in XML format and fill a NSMutableArray (altogether 34 items).

This data should be displayed in a TableView. I created a custom TableViewCell for that. However it always only displays the first 10 items (0-9), then the 11th item will change as you scroll up the table (starting from 11 and changing itself to 12, 13, 14 ...), then it will display the first 10 items again.

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {

    return [myCurrencies count];
}

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

        static NSString *myIdentifier = @"CurrencyDisplayCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];

        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"CurrencyTableViewCell" owner:self options:nil];
            cell = tableViewCell;
            cell.accessoryType = UITableViewCellAccessoryNone;
            self.tableViewCell = nil;
        }

        NSLog(@"%i",indexPath.row);

        actualRate.text = [NSString stringWithFormat:@"%@",[myRates objectAtIndex:indexPath.row]];
        currencyCode.text = [NSString stringWithFormat:@"(%i) %@",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]];
        countryFlag.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.开发者_开发百科gif",[myCurrencies objectAtIndex:indexPath.row]]];

        return cell;
    }

However when I just set the textlabel of the standard cell everything is fine and I am displayed 34 table rows with numbers 0 - 33:

cell.textLabel.text = [NSString stringWithFormat:@"%i",indexPath.row]; 

Any idea out there?


your cell initialization is wrong

first of all name all your labels and image views in Interface Builder with tag for example actualRate - tag 101 currencyCode tag 102 countryFlag tag 103

then

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

        static NSString *myIdentifier = @"CurrencyDisplayCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];

        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CurrencyTableViewCell" owner:self options:nil] lastObject];
        }

        NSLog(@"%i",indexPath.row);

        [((UILabel *)[cell viewWithTag:101]) setText:[NSString stringWithFormat:@"%@",[myRates objectAtIndex:indexPath.row]]];
        [((UILabel *)[cell viewWithTag:102]) setText:[NSString stringWithFormat:@"(%i) %@",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]]];
        [((UIImageView *)[cell viewWithTag:103]) setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[myCurrencies objectAtIndex:indexPath.row]]]];

        return cell;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消