UIView子类的后续实例导致显示错误?

时间:2013-02-02 04:32:58

标签: iphone instance ticker

我制作了一个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];
}

//输出正常

enter image description here

但是当我制作第二个Ticker类实例时,它彼此重叠。

使用ticker.tag

管理两个实例的输出

With Two Instance

任何想法? 我该如何解决这个错误。 在此先感谢!

嘿我已经上传了一个例子,请检查Horizontal List

1 个答案:

答案 0 :(得分:2)

  1. 添加了objectArray2。
  2. 重复的BSE_sticker方法
  3. 在second_Sticker中
  4. 做了所有相同的BSE_Sticker代码,除了将它添加到objectArray2。
  5. 根据标记
  6. 在委托中返回stkticker2的objectARray2

    项目here

      

    编辑

    我调查了问题并解决了重复的实例/摇摆

    您的自动收报机课程使用static int count

      

    静态变量每个类只实例化一次。因此   关于计数器的编码导致对象检查例如   多次。

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

    然后它会正常工作