【发布时间】:2014-05-05 10:11:51
【问题描述】:
我已经查看并检查了多个有关将UIButton 添加到UITableViewCell 的问题,但不知何故我没能找到我所面临的确切问题。将UIButton 添加到UITableViewCell 时,单元格的文本标签与按钮重叠。该按钮位于该行的开头,后面应该是文本标签。我很可能做错了什么,但我找不到什么。这是我的cellForRowAtIndex 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[self._tableView dequeueReusableCellWithIdentifier:@"event"];
Event* event =[self eventForIndexPath:indexPath];
if(cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"event"];
[[cell textLabel] setText:event.customer.name];
if(event.startTime!=nil)
[[cell detailTextLabel] setText:event.startTime];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UIButton *cellButton =(UIButton*) [cell.contentView viewWithTag:CELL_BUTTON_TAG];
if(cellButton == nil){
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag=CELL_BUTTON_TAG;
[cellButton setFrame:CGRectMake(0.0f, 0.0f, 44.0f, cell.frame.size.height)];
[cellButton setImage:[UIImage imageNamed:@"van_green.png"] forState:UIControlStateNormal];
[cellButton setImage:[UIImage imageNamed:@"location.png"]forState:UIControlStateSelected];
[cellButton addTarget:self action:@selector(carButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cellButton.layer.borderWidth = 0.0;
[cell.contentView addSubview:cellButton];
}
return cell;
}
【问题讨论】:
-
你需要设置 cell.textlabel.frame 否则你放自定义 UILabel
-
使用此链接解决您的问题stackoverflow.com/questions/22862938/…
标签: ios uitableview uibutton