我一直在到处寻找,但还没有找到我的答案。
我使用 JSON 中的动态单元格填充 UITableView,并尝试隐藏任何额外的单元格。我关闭了IB中的分隔符,当然所有的单元格分隔符都消失了。如何在每个 tableviewcell 的底部和顶部添加一条线,以便只有包含信息的单元格显示边框?我已经导入了 Quartz 并一直在使用 CALayer 但找不到解决方案。
我在这里发现了类似的问题,但唯一的答案并不是很有帮助。
什么是更好的、不同的方式来做到这一点?
这是我的 cellForRowAtIndexPath 和 numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//set equal to the information in the array
return [_jsonDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//create Dictionary of data in row
NSDictionary *jsoninfo = [_jsonDataArray objectAtIndex:indexPath.row];
//get required keys from dictionary and assign to vairables
NSString *title = [jsoninfo objectForKey:@"title"];
NSString *subtitle = [jsoninfo objectForKey:@"subtitle"];
NSURL *imageURL = [NSURL URLWithString:[jsoninfo objectForKey:@"series_image_URL"]];
//download the images.
NSData *imgData = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:imgData];
//set boarder for custom cells... I need to have a border on the top and bottom of the cells I am creating so xcode does not autofill the empty space.
//fill in text to cells
cell.textLabel.text = title;
cell.detailTextLabel.text = subtitle;
cell.imageView.image = img;
return cell;
}
我也认为这不是最好的主意,但如果你真的想这样做,这里的代码将实现你想要的:
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// Draw top border only on first cell
if (indexPath.row == 0) {
UIView *topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)];
topLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:topLineView];
}
UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.bounds.size.height, self.view.bounds.size.width, 1)];
bottomLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:bottomLineView];
将此代码放入tableView:cellForRowAtIndexPath:
方法。你的最终样子UITableView
会是这样的:
请注意,这对于性能来说并不是很好,特别是当您有很多单元时。如果你的数据量比较大,可以参考这个问题 https://stackoverflow.com/questions/3128752/draw-line-in-uiview有关如何优化绘图的帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)