나는 초보자입니다. UITableView
을 만들었으며 아래 코드를 사용하여 특정 행 (예 : 두 번째 행)에 UIButton
을 추가했습니다. 내가 테이블보기를 스크롤 할 때 테이블에 버튼 복제가 나타납니다.uitableviewcell에서 버튼의 중복을 제거하는 방법?
미리 감사드립니다. 제게 해결책을주십시오.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
if (indexPath.row ==1) {
return height + (CELL_CONTENT_MARGIN * 2) + CELL_HEIGHT;
}
else{
return height + (CELL_CONTENT_MARGIN * 2);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
// [[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
if (indexPath.row==1) {
UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//Button.frame = CGRectMake(120, 120, 80, 24);
NSString *text [email protected]"BUTTON" ;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
Button.frame = CGRectMake(65, 150, 160, 24);
[Button addTarget:self
action:@selector(load)
forControlEvents:UIControlEventTouchUpInside];
Button.backgroundColor = [UIColor redColor];
Button.layer.cornerRadius = 5;
[Button setTitle:text forState:UIControlStateNormal];
[cell addSubview:Button];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
사용 [cell.contentview의 addSubview : 버튼]
맞춤 세포 대처하는 적절한 방법은 세포를 서브하고 우 인덱스를 요청한 해당 셀을 설정하는 것; –
보너스 주셔서 감사합니다 –
행복 한 하루를 보내고 있다면 –