【问题标题】:How to set different subview in every section on tableViewCell如何在 tableViewCell 的每个部分中设置不同的子视图
【发布时间】:2010-09-02 02:27:13
【问题描述】:

伙计们,我想在每个部分设置不同的子视图,我将不同的视图放入 NSMutableArray,所以我希望我可以根据 indexPath.section 访问它,这是我的代码:

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;
}

但它不起作用,子视图只出现在最后一部分(但该子视图的内容是正确的)。
我确定该数组已被不同的视图填充(通过执行 NSLog)。
任何人都可以帮助我.????

【问题讨论】:

  • 你的意思是第一部分的单元格没有出现子视图或者显示不正确?
  • 是的,第一节的单元格没有出现子视图,但最后一节的单元格出现了子视图

标签: iphone objective-c uiview uitableview


【解决方案1】:
  • 你在做什么?为什么将 int 除以 float 并将其存储在 y 中?

看起来您只有一个self.textView。您正在更改其.text 成员并将其添加到数组中多次,但它只是一个对象,并且在循环结束时您对该对象有多个引用,因此您所看到的只是它结束的文本。

【讨论】:

  • 对不起,我在我的代码中进行了很多实验,但顺便说一句,这里没有任何意义,我忘记清除它'.'v 你们是对的人,当我写的时候this NSLog(@"第一个索引:%@", [self.arrayPromotions objectAtIndex:0]); NSLog(@"第二个索引:%@", [self.arrayPromotions objectAtIndex:1]);它写的是相同的文本,:(所以我应该为这种情况分配多个文本视图。????
  • 是的,您需要为每个单独的视图。如果它只不过是一行文本,只需使用单元格的现有文本标签,但我希望你希望它比这更复杂。您当然可以释放您添加的子视图,因为使它们成为子视图会导致它们被父视图保留。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多