【问题标题】:subsequent instances of a UIView subclass causing display error?UIView 子类的后续实例导致显示错误?
【发布时间】:2013-02-02 04:32:58
【问题描述】:

我做了一个Stock Tiker 来显示连续的库存对象。并且一审工作正常。

票证代码的实现如下:

- (void)viewDidLoad
{
    tickerView=[[StockTiker alloc] init];
    [tickerView setFrame:CGRectMake(0, 0, 320, 20)];
    tickerView.delegate=self;

    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 20)];
    [label setBackgroundColor:[UIColor redColor]];
    label.text=@"First Object";

    UILabel *label2=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    [label2 setBackgroundColor:[UIColor grayColor]];
    label2.text=@"Second Object";

    UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    [label3 setBackgroundColor:[UIColor magentaColor]];
    label3.text=@"Third Object";


    UILabel *label4=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 20)];
    [label4 setBackgroundColor:[UIColor yellowColor]];
    label4.text=@"Fourth Object";

    UILabel *label5=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 20)];
    [label5 setBackgroundColor:[UIColor cyanColor]];
    label5.text=@"Fifth Object";


    viewArray=[[NSArray alloc] initWithObjects:label,label2,label3,label4,label5,nil];
    [self.view addSubview:tickerView];


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark- Ticker Delegate..

-(UIView*)viewForRow:(int)row inTicker:(StockTiker*)stock_Ticker
{
  return [viewArray objectAtIndex:row];
}

-(int)numberOfRowsForStockTiker:(StockTiker*)stock_Ticker
{
    return [viewArray count];
}

//输出没问题

但是当我创建 Ticker 类的第二个实例时,它彼此重叠。

使用ticker.tag管理两个实例的输出

有什么想法吗? 我该如何解决这个错误。 提前致谢!

嘿,我上传了一个例子,请查看Horizontal List

【问题讨论】:

  • 您没有真正提供足够的信息来正确调试它。如果要创建此 Ticker 类的两个实例,那么实例化第二个实例的代码在哪里?那里可能存在问题,除了您在某处有“静态”声明的可能问题(这意味着与此类的所有实例共享此变量)
  • 检查上面给出的文件链接。这包含带有静态变量的 stockTicker 文件

标签: iphone instance ticker


【解决方案1】:
  1. 添加了 objectArray2。
  2. 重复的 BSE_sticker 方法
  3. 在 second_Sticker 中,除了将 BSE_Sticker 添加到 objectArray2 之外,所有代码都与 BSE_Sticker 相同。
  4. 根据标签返回委托中stkticker2的objectARray2

项目here

编辑

我研究了问题并解决了重复实例/摆动

您的股票代码类使用static int count

每个类只实例化一次静态变量。因此 例如,您对计数器进行编码会导致对象检查 多次。

您应该将静态变量更改为普通 ivar,并在 start 方法中将其实例化为 0。

然后就可以正常使用了

【讨论】:

  • 这不是很好的答案,再次检查没有任何内容显示清晰且相互覆盖。我在屏幕截图中显示的内容
  • 现在可以工作了,但仍然不稳定。不像单个实例。
  • 如果可以的话,请把你的整理停止小摇摆
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
相关资源
最近更新 更多