-3

单击单元格时,添加到单元格的标签看起来像附图中的样子,为什么会这样???

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

这是代码:

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

{
静态 NSString *CellIdentifier = @"Cell";

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:@"profile_pic_bg@2x.png"]];

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;

}

4

1 回答 1

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];
        }
    }
于 2013-04-27T14:54:37.393 回答