此代码的目的是将订单中的项目添加到表格视图中,然后在表格底部画一条看起来像收据底部的线。这两件事分别起作用,但当结合在一起时,表是不可见的。
这是我的drawRect方法
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 0.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
int bottom = rect.size.height;
CGContextMoveToPoint(context, 0, bottom);
for (int i=0; i<rect.size.width; i = i + 20) {
CGContextAddLineToPoint(context, i + 10, bottom - 10);
CGContextAddLineToPoint(context, i + 20, bottom);
}
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
这是我的视图DidLoad
- (void)viewDidLoad {
[super viewDidLoad];
id item1 = @{@"item":@"Burger", @"cost":@"$3.50"};
id item2 = @{@"item":@"Fry", @"cost":@"$1.50"};
items = [[NSMutableArray alloc] init];
[items addObject: item1];
[items addObject: item2];
[orderItemsTable reloadData];
[self adjustHeightOfTableview];
DrawReceipt *receipt = [[DrawReceipt alloc] initWithFrame:orderItemsTable.frame];
receipt.backgroundColor = [UIColor whiteColor];
[self.view addSubview:receipt];
// Do any additional setup after loading the view.}
相反,您可以将行数增加1。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return receiptRows + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == receiptRows) {
return 1;
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == receiptRows) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bottom-line"];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
return yourOriginalCells;
}
编辑:如果你想滚动你的内容,但最后一行保持不变,怎么样:
- (void)viewDidLoad {
myTableView.frame = CGRectMake(0, 0, 100, 100);
UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 1)];
[bottomLine setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:bottomLine];
}
如果你想让你的背景低于UITableView
,但你的底线高于它,他们需要在两个不同的视图中。