subsequent instances of a UIView subclass causing display error?

一个人想着一个人 提交于 2019-12-11 05:54:31

问题


I made a Stock Tiker to display continuous stock objects. And Working fine for first Instance.

The implementation of the ticket code is as follows:

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

//Output is fine

But when I made second Instance of Ticker class it overlapped to each other.

Output with two instance managed using ticker.tag

Any Idea? How can I solve this error. Thanks in Advance!

Hey I have uploaded an example please check it Horizontal List


回答1:


  1. Added objectArray2.
  2. Duplicated BSE_sticker method
  3. in second_Sticker did all the same code of BSE_Sticker except add it to objectArray2.
  4. Return objectARray2 for stkticker2 in delegate based on tag

Project here

Edit

I looked into issue and solved repeating instance/ wobbling

Your ticker class uses static int count.

Static variables are instantiated only once per class. and therefore you coding regarding counter leads the object to check for instance multiple times.

you should change the static variable to a normal ivar and instantiate it to 0 in start method.

Then it will work fine



来源:https://stackoverflow.com/questions/14658616/subsequent-instances-of-a-uiview-subclass-causing-display-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!