簡體   English   中英

UITableView自定義單元中的問題

[英]Problem in UITableView custom cell

我正在嘗試向UITableView中的單元格添加自定義標簽。 當我嘗試執行此操作時,顯示屏混亂了,我無法弄清楚到底發生了什么。 請找到下面的圖片,我將發布我編寫的方法。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //cell.textLabel.font=[UIFont systemFontOfSize:16];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
    NSArray *contactSection=[contactNames objectForKey:contact];
    NSMutableArray *sugar=[db sugarId];

    if (isSearchOn) { 
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; 
        NSArray *splitText = [cellValue componentsSeparatedByString:@":"];
        NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
        //NSString *result=[contactText stringByAppendingString:[sugar objectAtIndex:[indexPath row]]];
        firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];                                                  
        firstLabel.tag =40; //This should be a constant probably
        firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
        firstLabel.text = contactText;
        [firstLabel sizeToFit];
        [cell.contentView addSubview:firstLabel];

        sugarLabel = [[UILabel alloc] initWithFrame:
                      CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
        sugarLabel.tag =40; //This should be a constant probably
        //sugarLabel.font = [UIFont boldSystemFontOfSize:16];
        sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
        [sugarLabel setHidden:YES];        
        [cell.contentView addSubview:sugarLabel];


        cell.textLabel.text =firstLabel.text;
        } else {
         NSString *cellText = [contactSection objectAtIndex:[indexPath row]];

            // split the text by the : to get an array containing { "AAA", "BBB" }
            NSArray *splitText = [cellText componentsSeparatedByString:@":"];

            // form a new string of the form "BBB AAA" by using the individual entries in the array
            NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];

            firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];                                                  
            firstLabel.tag =40; //This should be a constant probably
            //firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
            firstLabel.text = contactText;
            [firstLabel sizeToFit];
            [cell.contentView addSubview:firstLabel];

            sugarLabel = [[UILabel alloc] initWithFrame:
                                    CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
            sugarLabel.tag =40; //This should be a constant probably
            //sugarLabel.font = [UIFont boldSystemFontOfSize:16];
            sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
            [sugarLabel setHidden:YES];        
            [cell.contentView addSubview:sugarLabel];

            NSString *result=[NSString stringWithFormat:@"%@ %@",firstLabel.text,sugarLabel.text];
            cell.textLabel.text=result;

    }

替代文字

圖2:這是對BP建議的代碼的修改。 結果在這里。 替代文字

編輯:

每當我嘗試編寫stringWithFormat或以上語句時,內存都不會釋放,並且標簽相互重疊。 有沒有辦法解決這個問題..請幫助我..我花了半天的時間來解決這個問題,但是沒有運氣

如果要具有自定義單元格,建議您將UITableViewCell子類化。 在給定的示例代碼中,每次tableview在索引路徑中向單元格請求一行時,都會創建一個新標簽。 在自動釋放它們的同時,還向它們添加了單元格的contentview,這增加了它們的保留計數並確保了它們的壽命與tableview一樣長。

隨着單元的重用,您將繼續在其內容視圖中添加越來越多的標簽,直到最終用盡內存。

至於奇怪的文本輸出,似乎您正在使用格式調用將UILabel添加到您的一個字符串中。

如果要執行類似下面的代碼的操作,則會看到類似的內容。

UILabel *label = [[UILabel alloc] init];
NSLog(@"%@", label);
[label release];

編輯:看到已編輯的屏幕截圖,我假設您向下滾動到“ S”部分。 在這種情況下,您會看到其他標簽上方的標簽。 就像我提到的那樣,您每次都會創建一個新標簽,並將其放在您第一次使用該單元格(以及第二,第三次等)創建的標簽上。

獲得UILabel:...項的原因是方法的最后一行,您可以在其中設置cell.textLabel.text。 如果要在單元格中創建自己的字段,則不需要執行此操作。

NSString *result=[NSString stringWithFormat:@"%@ %@",contactText,sugarLabel]

應該讀:

NSString *result=[NSString stringWithFormat:@"%@ %@",contactText,sugarLabel.text]

您正在傳遞UILabel而不是NSString

編輯

嘗試實例化並僅在if (cell == nil) {塊中添加UILabels。

在訪問標簽屬性並在此塊外寫入它時可以

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM