2015-09-06 55 views
1

我有2個標籤。關於iCarousel和標籤iOS

名爲「標籤」的第一個標籤放置在傳送帶內的每個視圖內。標籤的字符串/文本是視圖的索引。

label.text = [[items1 objectAtIndex:index] stringValue]; 

我還有一個名爲「outsideLabel」的第二個標籤(傳送帶之外)。 我想讓outsideLabel的字符串/文本成爲視圖的索引(總是視圖在旋轉木馬前面)。

outsideLabel.text = [[items1 objectAtIndex:index] stringValue]; 

不知怎的,我做錯了,不知道我應如何以顯示outsideLabel的字符串/文本正確的數量(總是在前面是視圖)的代碼這一點。代碼有點顯示正確的數字,但在旋轉木馬中向後滾動時會變得混亂。轉盤類型是timeMachine。

我當前的代碼:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view 
{ 
    //create new view if no view is available for recycling 
    if (view == nil) 
    { 
     view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; 

     view.contentMode = UIViewContentModeCenter; 
     label = [[UILabel alloc] initWithFrame:view.bounds]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textColor = [UIColor whiteColor]; 

     if (carousel == carousel1) 
     { 

     CGRect test = CGRectMake(10, 10, 20, 20); 
     self.label.frame = test; 

     } 
     else { 
      CGRect test = CGRectMake(50, 40, 40, 40); 
      self.label.frame = test; 

      } 
     [view addSubview:label]; 

    } 
    else 
    { 
     label = [[view subviews] lastObject]; 
    } 

    if (carousel == carousel1) 
    { 
     //items in this array are numbers 
     outsideLabel.text = [[items1 objectAtIndex:index] stringValue]; 

     label.text = [[items1 objectAtIndex:index] stringValue]; 

     ((UIImageView *)view).image = [UIImage imageNamed:[view1background objectAtIndex:index]]; 

    } 
    else 
    { 

     //not relevant.... 
    } 

    return view; 
} 
+0

你試過iCarousel的委託方法 - - (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel?嘗試在這裏設置值..希望這可能會幫助你 – Nayan

回答

0

是你想要的「時間機器」式的旋轉木馬在我看來。我看不到您的代碼在任何地方設置輪播類型。你不需要設置旋轉木馬類型嗎?

+0

我在viewDidLoad方法中設置類型。是的,它已經是時間機器了。不過我只看到1張卡而不是幾張卡。 – user1293618

+0

你看過iCarousel演示程序嗎?它們包括Time Machine外觀的工作演示。 (自從我設立了一個新的iCarousel已經有一段時間了,所以我不記得頭腦中的細節。) –

2

從你提供的代碼看來,你並沒有在正確的位置初始化outsideLabel。爲了安全起見,您應該初始化您檢查視圖是否爲nil的塊內的所有子視圖。另一個安全的約定是爲所有子視圖分配標記,以便稍後可以從重用的視圖中檢索它們,如下面的代碼所示。爲了便於參考,並避免錯誤,我在我的實現文件的頂部定義這些標籤常量,像這樣:

#define INSIDE_LABEL_TAG 1 
#define OUTSIDE_LABEL_TAG 2 

這是安全得多,因爲它不依賴於視圖的結構,因爲是你的代碼,你在哪裏得到的最後一個鑑於案情:

label = [[view subviews] lastObject]; 

嘗試初始化outsideLabel該塊內,並使用標籤。在初始化中使用的模式是相同的,在一個UITableViewDataSource代表用於UITableView細胞的子視圖:

(UITableViewCell * _Nonnull)tableView:(UITableView * _Nonnull)tableView 
       cellForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath 

下面是一些僞代碼,其中顯示出我會使用標記和初始化outsideLabel

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view 
{ 
    //create new view if no view is available for recycling 
    if (view == nil) 
    { 
     //Configure the view 
     ... 

     /* Initialize views for all carousels */ 

     //Initialize the insideLabel and set its tag 
     ... 
     insideLabel.tag = INSIDE_LABEL_TAG; 

     //Initialize the outsideLabel and set its tag 
     ... 
     outsideLabel.tag = OUTSIDE_LABEL_TAG; 

     if (carousel == carousel1) 
     { 
      //Do any carousel-specific configurations 
     } 

     //Add all subviews initialized in this block 
     [view addSubview:label]; 
     [view addSubview:outsideLabel]; 
    } 
    else 
    { 
     //Get the subviews from an existing view 
     insideLabel = (UILabel *)[view viewWithTag:INSIDE_LABEL_TAG]; 
     outsideLabel = (UILabel *)[view viewWithTag:OUTSIDE_LABEL_TAG]; 
    } 

    if (carousel == carousel1) 
    { 
     //Set the values for each subview 
    } else { 
     //Other carousels... 
    } 

    return view; 
}