Search code examples
iosuitableviewdidselectrowatindexpath

UITableview - didSelectRowAtIndexPath shows different data issue


I am parsing RSS to UITableview and the data looks as it should but when I select one of the field in the UITableview it opens different data then what it should. Also when I scroll the tableview the same field that I have selected gives again different data.

So please where could I made my mistake?

The RSS link: http://www.luaikalkatawi.me/picksound

Thanks from now.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *trimed = [[containerObject.track init] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSLog(@"The link is %@", trimed);
}

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

ImgCell *cell = (ImgCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:nil options:nil];
    cell = (ImgCell*)[nib objectAtIndex:0];
}

containerObject = [objectCollection objectAtIndex:indexPath.row];//use the common object again for holding the corresponding array object

///// Image reloading from HJCacheClasses
[cell.img clear];
NSString *str1 = containerObject.image; //we use image property of rss object to set the image
NSString *trimmedString = [str1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *url = [NSURL URLWithString:trimmedString];
cell.img.url = url; //set the url to img view
[self.imgMan manage:cell.img];

cell.mainText.text = containerObject.title; //title member variable for setting title
cell.detailText.text = containerObject.description;//desc variable foe
return cell;
}

Solution

  • I don't know why this is confusing to you, but the correct containerObject, should be accessed in the didSelectRowAtIndexPath.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        containerObject = [objectCollection objectAtIndex:indexPath.row];
        NSString *trimed = [[containerObject.track init] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
        NSLog(@"The link is %@", trimed);
    }