2013-04-27 68 views
-3

單擊單元格時,添加到單元格的標籤看起來就像附圖中所示,爲什麼?UITableViewCell上的UILabel

http://s23.postimg.org/x4a7ffd7v/Untitled.png

這是代碼:

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

{
靜態的NSString * CellIdentifier = @ 「小區」;

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

int section = [indexPath section]; 
int row = [indexPath row]; 

int rowsInLastSections = 0; 
for (int i = 0; i < section; i++) { 
    rowsInLastSections += [self tableView:tableView numberOfRowsInSection:i]; 
} 

Friend *friend = [friendsArray objectAtIndex:row + rowsInLastSections]; 

[friend setIndexPathInTableView:indexPath]; 

NSString *firstName = [[NSString alloc]initWithFormat:@"%@", [friend firstName]]; 
NSString *lastName = [[NSString alloc]initWithFormat:@"%@", [friend lastName]]; 

UIImageView *profileImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)]; 
[profileImageView setImageWithURL:[NSURL URLWithString:[friend imageUrl]] placeholderImage:[UIImage imageNamed:@"[email protected]"]]; 

UILabel *firstNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, 230, 20)]; 
UILabel *lastNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 35, 230, 20)]; 
[firstNameLabel setText:firstName]; 
firstNameLabel.backgroundColor = table.backgroundColor; 
[lastNameLabel setText:lastName]; 
lastNameLabel.backgroundColor = table.backgroundColor; 

[cell.contentView addSubview:profileImageView]; 
[cell.contentView addSubview:firstNameLabel]; 
[cell.contentView addSubview:lastNameLabel]; 

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
[cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 

return cell; 

}

+0

請提供更多的細節。一些代碼會很好... – atxe 2013-04-27 14:47:54

+3

你應該爲各種'UITableViewDelegate'和'UITableViewDataSource'方法發佈你的代碼。 – jszumski 2013-04-27 14:48:04

+0

這是將子視圖添加到單元格的代碼:: http://pastie.org/7729414 – assafey 2013-04-27 15:27:00

回答

1

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

添加此代碼,

for(UIView *view in cell.subviews){ 
     if([view isMemberOfClass:[UILabel class]]){ 
      [(UILabel *)view removeFromSuperview]; 
     } 
    } 
+0

編輯帖子「首先,我應該說你這樣做的方式不好」我該怎麼做? – assafey 2013-04-27 15:19:12

+0

我不在didSelectRowAtIndexPath方法中添加uilabels或任何其他組件 – assafey 2013-04-27 15:25:48

+0

我根據您的代碼更改了答案,請檢查新答案。 – 2013-04-27 16:03:47