我有一个定制的UITableViewCell
.这些有以下问题:
蓝色突出显示的状态跨越整个屏幕宽度,我希望它只是设置在我的弯曲单元格背景的范围内。
有没有办法解决这两个问题?谢谢。
编辑:我的代码如下设置背景(只是一个自定义方法):
- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;
if (rowIndex == 0) {
background = [UIImage imageNamed:@"cell_top.png"];
} else if (rowIndex == rowCount - 1) {
background = [UIImage imageNamed:@"cell_bottom.png"];
} else {
background = [UIImage imageNamed:@"cell_middle.png"];
}
return background;
}
然后我在
reloadTable
中调用此方法void 方法,它只获取每个单元格并在行更新后对其进行更新(添加或删除一行,因为表格有 3 个不同的图像)。在我的 fetched results Controller 从 Core Data 获取我的所有项目并第一次加载表之后,我也会调用它。编辑 2:这是
reloadTable
的代码方法:- (void)refreshTable
{
for (NSInteger j = 0; j < [self.tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [self.tableView numberOfRowsInSection:j]; ++i)
{
UIImage *background = [self cellBackgroundForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
cell.backgroundView = cellBackgroundView;
}
}
}
问题是:我不知道如何设置表格 View 或表格 View 单元格的边距或宽度。我需要子类化一些东西吗?一般是怎么做的?
最佳答案
问题是您的表格 View 框架宽度,所选单元格的蓝色将填满整个单元格,因此您应该减小表格 View 的宽度,以便它有一些 margins
这样delete
按钮也会出现在左边一点。但是当您选择它们时,第一个和最后一个单元格仍然会有问题,因为它们具有圆角但蓝色选择不会有圆角所以我建议您应该为选定状态创建一个 .png 图像(一个第一个单元格,一个用于中间单元格,一个用于底部单元格)
编辑
在 iOS 中,margins
的概念不存在,我只是使用该术语,因为我认为它会更容易理解,但在这里你必须做些什么才能在表格 View 的左右显示一点空间:
yourTableView.frame = CGRectMake(leftMarginSpace,
0,
self.view.frame.size.width
- leftMarginSpace
- rightMarginSpace,
self.view.frame.size.height);
因此,如果您将 TableView 添加到 View Controller 的 View 中,那么 TableView 将位于
leftMarginSpace
距左侧的距离,位于 View Controller 的顶部(0 y 位置),位于 rightMarginSpace
与右侧的距离并将具有 View Controller 的高度。如果您将 TableView 作为 subview 添加到另一个 View (不是 View Controller 的 View ),那么您必须像这样更改框架:
yourTableView.frame = CGRectMake(leftMarginSpace,
0,
parentView.frame.size.width
- leftMarginSpace
- rightMarginSpace,
parentView.frame.size.height);
我还注意到你想创建一个分组的表格 View 样式,所以你应该检查
grouped table view
.
关于ios - 自定义 UITableViewCell 的 UI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597729/