gpt4 book ai didi

ios - 滑动删除按钮位于自定义 UITableViewCell 文本下方

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:17 25 4
gpt4 key购买 nike

我在使用自定义 UITableViewCell 滑动删除按钮时遇到问题。当我滑动它时,删除按钮位于单元格中的文本下方。您可以在下面看到我正在使用的图像和代码。

注意:我在xib中绘制tableviewcell。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"HareketCell";

HareketCell *cell = (HareketCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HareketCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

[cell.lblDakika setText:[NSString stringWithFormat:@"%@%@", [[arrProgramim objectAtIndex:indexPath.row]valueForKey:@"dakika"], @" dk"]];

[cell.imgThumbnail setImage:[UIImage imageNamed:@"most_harita_icon.png"]];

return cell;
}

enter image description here

最佳答案

那是因为您没有将 UILabel 添加为 UITableViewCellcontentView 的 subview ,而是将其添加为 UITableViewCell 的 subview UITableViewCell 本身。

当您想要子类化 UITableViewCell 并向其添加 subview 时,您应该始终将它们添加到单元格的 contentView 中。

这是来自 Apple 的文档 UITableViewCell :

If you want to go beyond the predefined styles, you can add subviews to the contentView property of the cell.

如果您这样做,iOS 会自动调整 contentView 中的所有内容以显示删除按钮并且没有任何重叠。

更新:

从下面的评论来看,单元格是在单独的 .xib 上创建的,因此标签已经是单元格 contentView 的 subview

问题是在 -tableView:cellForRowAtIndexPath: 中加载 NIB。您必须事先在 TableView 上注册 NIB,例如,在 -viewDidLoad 中。像这样:

- (void)viewDidLoad {
// Assuming the xib is named HareketCell.xib
UINib *nib = [UINib nibWithNibName:@"HareketCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"HareketCell"];
}

现在,为此更改 -tableView:cellForRowAtIndexPath::

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"HareketCell";

HareketCell *cell = (HareketCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

[cell.lblDakika setText:[NSString stringWithFormat:@"%@%@", [[arrProgramim objectAtIndex:indexPath.row]valueForKey:@"dakika"], @" dk"]];

[cell.imgThumbnail setImage:[UIImage imageNamed:@"most_harita_icon.png"]];

return cell;
}

你看,-dequeueReusableCellWithIdentifier 已经为你自动加载了 NIB,如果你事先注册的话。

关于ios - 滑动删除按钮位于自定义 UITableViewCell 文本下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com