【问题标题】:UIButton and TextLabel overlapping in UITableViewCellUITableViewCell 中的 UIButton 和 TextLabel 重叠
【发布时间】: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;

}

【问题讨论】:

标签: ios uitableview uibutton


【解决方案1】:

抱歉,您的代码完全错误..您使用的方式不正确,而且您每次都在添加一个按钮。

tableViewCell重用,因此您必须在初始化单元格时添加按钮和其他 UI 元素,而不是每次,否则您的应用将在内存警告、崩溃、不流畅等

做你想做的事情的正确方法是创建你的CustomTableViewCell 子类UITableViewCell 并添加你的标签和按钮。真的很简单:

1) 创建一个扩展UITableViewCell的新文件CustomSearchCell对象:

CustomSearchCell.h

#import <UIKit/UIKit.h>

@interface CustomSearchCell : UITableViewCell

@property (strong, nonatomic) UIButton *button;
@property (strong, nonatomic) UILabel *lblDetails;

@end

CustomSearchCell.m

#import "CustomSearchCell.h"

@implementation CustomSearchCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        _lblDetails = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 200, 45)];
        [_lblDetails setFont:[UIFont systemFontOfSize:20.0]];
        [_lblDetails setTextColor:[UIColor blackColor]];

        _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, self.frame.size.height)];

        [self.contentView addSubview: _lblDetails];
        [self.contentView addSubview: _button];
    }
    return self;
}

@end

2) 在您的视图控制器中:

#import "CustomSearchCell.h"

和:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"ListCellIdentifier";
    CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[CustomSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Settings

    return cell;
}

另一个问题是当你按下按钮时。您将收到发送者,但是当您拦截触摸时,tableViewCell 将可用于不同的 indexPath。小心。正确的方法是另一种方法。当您按下按钮时,您需要以某种方式保存实际的 indexPath。 ..

【讨论】:

  • 如果我们使用customcell的方式,则不需要这行(addSubview:)[self.contentView addSubview: _lblDetails];。我们可以直接在xib中添加。
  • 最后,子类化 UITableViewCell 是最好的选择
【解决方案2】:

问题出在这一行[cell.contentView addSubview:cellButton];。当您的单元格将被重用时,该单元格已包含单元格按钮,您尝试再次添加该按钮。只需执行以下步骤即可解决此问题。

1) 添加按钮标签以从基本视图中获取。

UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag = CELLBUTTONTAG;

2) 检查条件视图标签并将其添加到基础视图中,如下所示。

UIButton *cellButton = [cell.contentView viewWithTag:CELLBUTTONTAG];
if(cellButton == nil)
{
  UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
  cellButton.tag = CELLBUTTONTAG;
  ...
  [cell.contentView addSubview:cellButton];
}

【讨论】:

  • 我只是尝试过这种方式并编辑了问题中的代码,它没有达到预期的效果。该按钮隐藏了部分文本视图文本标签
  • @Georgi 试试另一个框架。 (只需将其添加到单元格的右侧)如果您无法添加(相互碰撞),请使用自定义单元格。
  • 我刚刚使用了一个自定义单元格,它更简单一些 :) 谢谢!
【解决方案3】:

您已将单元格按钮的 y 位置指定为 0.0f。这是 y 重叠。尝试根据需要更改一些值。

[cellButton setFrame:CGRectMake(0.0f, 0.0f, 44.0f, cell.frame.size.height)];

[cellButton setFrame:CGRectMake(0.0f, some big value, 44.0f, cell.frame.size.height)];

【讨论】:

  • 按钮和文本标签在水平方向上重叠,因此更改单元格按钮的 y 位置不会得到预期的效果
  • 然后改变 x 位置伙计
  • 喜欢标签宽度+一些偏移量=按钮x位置
  • 我试过了,然后按钮更多地移动到文本标签中。该按钮位于该行的开头,后面应该是文本标签。似乎最好的办法是继承UITableViewCell 并创建一个自定义单元格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
相关资源
最近更新 更多