Objective c 如何创建此类徽章?
我画了这个原型: 左边的数字是项目优先级,列表可以重新排序,因此值将发生变化。如何创建这些位于左侧的徽章?如果可能的话,我更喜欢代码方法,而不是PNG图像 更新: 请看一看:Objective c 如何创建此类徽章?,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我画了这个原型: 左边的数字是项目优先级,列表可以重新排序,因此值将发生变化。如何创建这些位于左侧的徽章?如果可能的话,我更喜欢代码方法,而不是PNG图像 更新: 请看一看: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UI
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect priorityLabelRect = CGRectMake(10, 5, 20, 30);
UILabel *priorityLabel = [[UILabel alloc] initWithFrame:priorityLabelRect];
priorityLabel.layer.cornerRadius = 5.0f;
priorityLabel.layer.backgroundColor = [[UIColor grayColor] CGColor];
priorityLabel.tag = kPriorityValueTag;
[cell.contentView addSubview:priorityLabel];
[priorityLabel release];
CGRect meatLabelRect = CGRectMake(80, 5, 300, 30);
UILabel *meatLabel = [[UILabel alloc] initWithFrame:meatLabelRect];
meatLabel.tag = kMeatValueTag;
[cell.contentView addSubview:meatLabel];
[meatLabel release];
cell.showsReorderControl = YES;
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
但优先级周围没有显示灰色区域。
如果我添加一个新的,优先级周围会出现一个灰色区域,时间不到一秒钟,但它会立即消失。更不用说优先级值不位于灰色区域的中心
知道我做错了什么吗?在单元格中添加UILabel,并根据您的喜好设置格式。对于圆角,设置label.layer.cornerRadius。将UILabel添加到单元格中,并根据您的喜好设置其格式。对于圆角,设置label.layer.cornerRadius。您可以创建一个组合控件。它是一个UIImageView,其中UIImage和UILabel作为子视图 一个常规对象,其UIImageView.image带有灰色矩形,然后UILabel表示白色数字。然后只需更改标签文本的值
如果您从UIImageView及其子类开始,您将能够直接将其粘贴到常规UITableViewCells中,因为它们已经为UIImageView留出了空间。您可以创建一个组合控件。它是一个UIImageView,其中UIImage和UILabel作为子视图 一个常规对象,其UIImageView.image带有灰色矩形,然后UILabel表示白色数字。然后只需更改标签文本的值 如果从UIImageView及其子类开始,就可以直接将其粘贴到常规UITableViewCells中,因为它们已经有了一个UIImageView的空间 为徽章创建UIView。 看这里,它将解释如何绘制你想要的徽章形状。并画出号码: 创建一个将接收单元格编号的变量,并将其绘制在单元格中 应该是这样的-
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(10,10,40,40);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextFillRect(context, rectangle);
CGPoint point =CGPointMake(20,20);
[self.cellNumber drawAtPoint:point withFont:font];
}
4将徽章视图添加到单元格中,并在CellForRawAtIndexPath中将数字传递到徽章视图
祝你好运
为徽章创建UIView。
看这里,它将解释如何绘制你想要的徽章形状。并画出号码:
创建一个将接收单元格编号的变量,并将其绘制在单元格中
应该是这样的-
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(10,10,40,40);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextFillRect(context, rectangle);
CGPoint point =CGPointMake(20,20);
[self.cellNumber drawAtPoint:point withFont:font];
}
4将徽章视图添加到单元格中,并在CellForRawAtIndexPath中将数字传递到徽章视图
祝你好运尽管这种方法可行,但我还是推荐分层方法,因为它几乎肯定会更快。尽管这种方法可行,但我还是推荐分层方法,因为它几乎肯定会更快。