2012-05-23 22 views
-1

我是iPhone開發新手。我正在開發一個應用程序,其中包含單元格內的標籤tableview。我已經爲每一行放置了不同的標籤,就像這樣,對於每個細胞行我都這樣做了。但是當我滾動tableview時,我的標籤變得混雜了。請幫幫我!如何控制我的tableview單元格標籤?

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

if (indexPath.row == 2) 
{ 
    pendingListNumberLabel = [[UILabel alloc] init]; 
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 
    pendingListNumberLabel.textColor = [UIColor orangeColor]; 
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
    [cell addSubview:pendingListNumberLabel]; 
} 

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row]; 
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
return cell; 
+4

後...你....代碼! – borrrden

+0

多一點解釋和一些代碼wud幫助我們更好地幫助你。 :) –

+0

如果沒有看到代碼,我會認爲你使用的是不正確的重用標識符。 –

回答

0

您正在將UILabel添加爲子視圖,這是此處的錯誤。 通常你會使用自定義的UITableViewCell對於這種東西,但速戰速決是這樣的:

if (indexPath.row == 2) 
{ 
    pendingListNumberLabel = [[UILabel alloc] init]; 
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 
    pendingListNumberLabel.textColor = [UIColor orangeColor]; 
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
    pendingListNumberLabel.tag = 1337 
    [cell addSubview:pendingListNumberLabel]; 
} else { 
    for (UIView * subView in [cell subviews]) { 
     if ([subView tag] == 1337) { 
      [subView removeFromSuperview]; 
     } 
    } 
} 
+0

謝謝你的幫助。 – jone

-1

爲了解決這個問題,你可以把cell=nil;檢查cell== nil條件之前或創造細胞每次。它會工作但它不好習慣

這也爲文本

靜態的NSString * CellIdentifier = @ 「細胞」 工作; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];}}}}

cell = nil;

如果(細胞==無)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

}

如果(indexPath.row == 2){

pendingListNumberLabel = [[UILabel alloc] init]; 

pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 

pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 

pendingListNumberLabel.textColor = [UIColor orangeColor]; 

pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 

[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
[cell addSubview:pendingListNumberLabel]; 

}

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];

[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

return cell;

0

發生這種情況,因爲你正在使用可重複使用的細胞,而不是在一個正確的方式..

只需更換你的代碼 -

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

本 -

UITableViewCell *cell = [[UITableViewCell alloc] init]; 

上面的代碼可以解決你的問題,但是如果有大量的單元格,這並不好。在這種情況下,你應該使用可重用的單元格。

+0

提供的同一個單元格,而且它正在使用此代碼。但是iam從服務器獲取標籤文本。我已經在viwedidload中爲Web服務編寫了代碼。現在我的標籤文字沒有更新。 – jone

0

在你tableView:cellForRowAtIndexPath:方法,首先要使選定行這樣

NSUInteger row = [indexPath row]; 

然後用「行」,以從對應的tableView行的NSArray抓住你的數據,並將其分配給您的單元格的爲textLabel的文本屬性這樣

NSString *string = [NSString stringWithFormat:@"%@",self.allItems objectAtIndex:row]; 
cell.textLabel.text = string; 
return cell; 

當然你已經創建了一個UITableViewCell與重用標識符是這樣的:

static NSString *TableIdentifier = @"TableIdentifier"; 

UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:TableIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleValue1 
      reuseIdentifier:TableIdentifier]; 
} 

關鍵是使用與您的表格行完全對應的數組。 (即 - 數組中的項目0對應於tableView中的第0行),那麼你不會出錯。

0
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
else 
     { 
      UILabel *titleLbl = (UILabel *)[cell viewWithTag:1]; 
      [titleLbl removeFromSuperview]; 
     } 

if (indexPath.row == 2) 
{ 
    pendingListNumberLabel = [[UILabel alloc] init]; 
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 
    pendingListNumberLabel.textColor = [UIColor orangeColor]; 
    pendingListNumberLabel.tag = 1;// Write here..................................... 
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
    [cell addSubview:pendingListNumberLabel]; 
} 

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row]; 
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
return cell; 

我認爲這對你有幫助。

0

您只需通過省略if (cell == nil)檢查修復標籤混合的問題。使用波紋管代碼:

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
//if (cell == nil) // Need not this checking 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

使用小區分配直接如上:)

相關問題