views:

40

answers:

1

guys i want to set different subview in every section, i put my different view into NSMutableArray, so i hope i can access it depend on indexPath.section, this is my code :

for (int i =0; i<promoCount; i++) {

    self.textView.text= [NSString stringWithFormat:@"text view :%d",i];
    [self.arrayPromotions addObject:self.textView];
}

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

    static NSString *cellPromotionIdentifier = @"cellPromo";
    cell = [tableView dequeueReusableCellWithIdentifier:cellPromotionIdentifier];
    if (cell==nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellPromotionIdentifier] autorelease];

    }
    [cell.contentView addSubview:[self.arrayPromotions objectAtIndex:indexPath.section]];
    cell.contentView.layer.masksToBounds = YES;
    cell.contentView.layer.cornerRadius = 5.0;


return cell;
}

But it doesn't work, subview just appear in the last section (but content of that subview is right).
iam sure that array have been filled by different view (by doing NSLog).
anybody can help me.????

+1  A: 
  • What are you doing with y? Why divide an int by a float and store it in y?

Looks like you have only one self.textView. You are changing its .text member and adding it to the array multiple times but it is only one object and at the end of the loop you have multiple references to that one object, so all you see is the text it ended up with.

Adam Eberbach
iam sorry, i have a lot of experiment in my code, but btw y is doesn't mean anything here, i forget to clear it '.'vyou're correct guys, when i write thisNSLog(@"first index : %@", [self.arrayPromotions objectAtIndex:0]);NSLog(@"second index: %@", [self.arrayPromotions objectAtIndex:1]);it's write a same text, :( so should i alloc multiple textView for this case.????
Imam Arief W
Yes you would need a separate view for each. If it is nothing more than a line of text just use the cell's existing text label but I expect you want it to be more complicated than that. You can release the subviews you add of course as making them a subview causes them to be retained by the parent view.
Adam Eberbach