小编How*_*ear的帖子

使用CGContextAddLineToPoint和CGContextSetLineWidth绘制一条非常细的线

我想在我的UIView的drawRect方法中绘制一条非常细的线条粗线.我看到的CGContextSetLineWidth值为0.5的行与用于绘制边框CALayer的相同1.0宽度值不匹配.

您可以看到两者之间的差异 - 红线(宽度= 1)比紫/蓝线(宽度= 0.5)薄得多.

用CALayer绘制的边框

这是我绘制伪1.0宽度水平线的方式:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextSetLineWidth(ctx, 0.5); // I expected a very thin line

CGContextMoveToPoint(ctx, 0, y);
CGContextAddLineToPoint(ctx, self.bounds.size.width, y);

CGContextStrokePath(ctx);
Run Code Online (Sandbox Code Playgroud)

这是相同视图的边框,这次使用1.0边框宽度:

UIView *myView = (UIView *)self;
CALayer *layer = myView.layer;
layer.borderColor = [UIColor redColor].CGColor;
layer.borderWidth = 1.0;
Run Code Online (Sandbox Code Playgroud)

我需要做些什么来绘制我自己的自定义行,其宽度与CALayer版本相同?

core-graphics drawrect ios ios5 ios6

15
推荐指数
2
解决办法
1万
查看次数

使用dispatch_once为类层次结构创建单例类

我有一个继承自'MyClass'的2个子类,每个子类应该是一个单例.

当我没有继承任何其他类时,我已经使用此模式获取静态实例:

+ (MyClass *)getInstance
{
    static dispatch_once_t once;
    static MyClass *instance;

    dispatch_once(&once, ^{
        instance = [[MyClass alloc] init];
    });

    return instance;
}
Run Code Online (Sandbox Code Playgroud)

这很有用.现在,如果我添加两个新的子类,FirstClass和SecondClass,它们都继承自MyClass,我如何确保返回相应的ChildClass?

dispatch_once(&once, ^{
    // No longer referencing 'MyClass' and instead the correct instance type
    instance = [[[self class] alloc] init];
});

FirstClass *firstClass = [FirstClass getInstance]; // should be of FirstClass type
SecondClass *secondClass = [SecondClass getInstance]; // should be of SecondClass type
Run Code Online (Sandbox Code Playgroud)

执行上述操作意味着我总是回到我实例化的第一类作为我的第二类类型:

first: <FirstClass: 0x884b720>
second: <FirstClass: 0x884b720>
// Note that the address and type …
Run Code Online (Sandbox Code Playgroud)

singleton multithreading objective-c class-hierarchy

8
推荐指数
1
解决办法
1464
查看次数

NSLocalizedString有时会加载字符串,而不是总是

NSLocalizedString只有一半的时间可以恢复所引用键的预期值.其他时候我回到NSLocalizedString中指定的密钥名称,并且在每次其他运行时都会一直发生.

目前,我目前只支持英语.

我打电话给:

NSString *someText = NSLocalizedString(@"mystring.keyname", nil);
Run Code Online (Sandbox Code Playgroud)

en.lproj/Localizable.strings的内容:

"mystring.keyname" = "Hello there!";
Run Code Online (Sandbox Code Playgroud)

当它不能正常工作时,someText's价值就是mystring.keyname.

以下是我测试可靠性的方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSLog(@"Strings file: %@", [bundle pathForResource:@"Localizable" ofType:@".strings"]);
    NSLog(@"Localizations: %@", [bundle localizations]);
    NSLog(@"Local Dict: %@", [bundle localizedInfoDictionary]);
    NSLog(@"localizedStringForKey: %@", [bundle localizedStringForKey:@"mystring.keyname"value:@"Wha happened?" table:nil]);
    NSLog(@"Localized String: %@", NSLocalizedString(@"mystring.keyname", @"Are you sure you want to start a new game?"));
}
Run Code Online (Sandbox Code Playgroud)

这会在每次运行时打印出预期值:

Run #1:
Local Dict: (null)
localizedStringForKey: Hello there!
Localized String: Hello there! …
Run Code Online (Sandbox Code Playgroud)

objective-c nslocalizedstring ios

7
推荐指数
1
解决办法
1785
查看次数

错误:'NSObject'没有可见的@interface声明选择器'copyWithZone:'

我想允许我的类对象的深层副本,并尝试实现copyWithZone,但调用[super copyWithZone:zone]产生错误:

error: no visible @interface for 'NSObject' declares the selector 'copyWithZone:'

@interface MyCustomClass : NSObject

@end

@implementation MyCustomClass

- (id)copyWithZone:(NSZone *)zone
{
    // The following produces an error
    MyCustomClass *result = [super copyWithZone:zone];

    // copying data
    return result;
}
@end
Run Code Online (Sandbox Code Playgroud)

我该如何创建这个类的深层副本?

copywithzone ios

4
推荐指数
1
解决办法
3549
查看次数

滚动后UITableView的单元格分隔符消失

我有一个UITableView配置为'UITableViewStylePlain',其UITableViewCellSeparatorStyleSingleLine用于其分隔符样式.细胞具有背景颜色.

问题是当您滚动桌面视图时,一旦某些单元格从屏幕上消失并被带回,分隔符将不再可见.

单元格注册为:

[tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
Run Code Online (Sandbox Code Playgroud)

细胞代码:

- (void)customizeMyTable
{
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    NSString *cellIdentifier = [MyTableViewCell cellIdentifier];
    UINib *nib = [UINib nibWithNibName:cellIdentifier bundle:nil];

    [self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
    self.tableView.rowHeight = 50;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellID];

    [cell configure:someDataForThisRow];

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor lightGrayColor];
}
Run Code Online (Sandbox Code Playgroud)

有人遇到这个问题吗?这似乎只发生在iOS 5上,而不是iOS 6 Tableviews上.

uitableview ios

2
推荐指数
1
解决办法
3937
查看次数