小编Pha*_*oan的帖子

如何在iOS中模仿UISearchBar的Safari行为

我有一个UISearchBartitleView导航栏(它看起来像下面的图片)的,我想使它的工作方式类似于一个在Safari浏览器.

我没有足够的声誉来发布图片,这是链接

第一张照片

在此输入图像描述

第二张照片

在此输入图像描述

我想要实现的目标是:

  • 在正常状态下,导航栏包含2个左右按钮,UISearchBar在该搜索栏内有一个清除按钮(看起来像带有2个加法按钮的第一张图片).
  • 在搜索状态中,view被另一个取代view,当它被解雇时,view返回到原始状态.
  • 适用于iOS 6和7

我知道这可以通过使用来完成,UISearchDisplayController但它不起作用.这是我的代码:

.h:实施TableView's DataSource/Delegate, UISearchDisplayDelegate, UISearchBarDelegate

.M

-(void) viewDidload
{
    //Add left, right buttons
    self.leftButton = [[UIBarButtonItem alloc] init];
    [self.leftButton setStyle:UIBarButtonItemStylePlain];
    [self.leftButton setTitle:@"Button"];
    self.navigationItem.leftBarButtonItem = self.leftButton;

    self.rightButton = [[UIBarButtonItem alloc] init];
    [self.rightButton setStyle:UIBarButtonItemStylePlain];
    [self.rightButton setTitle:@"Button"];

    self.navigationItem.rightBarButtonItem = self.rightButton;
    self.searchBar = [[UISearchBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
    if (DEVICE_IS_IOS7) {
        self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    }
    self.navigationItem.titleView = self.searchBar;
    self.searchBar.delegate = self; …
Run Code Online (Sandbox Code Playgroud)

objective-c uinavigationbar uisearchdisplaycontroller ios

10
推荐指数
1
解决办法
1459
查看次数

与透明标题的iOS UIButton在白色背景

我有一个UIButton透明背景和白色标题的自定义.我正在寻找一个简单的解决方案,以便在系统上实现高亮(白色背景和透明标题)时将其反转UISegmentedControl.

有没有比在用作CALayer掩码的快照上反转alpha更简单的解决方案?

如果没有,你能告诉我怎样才能反转CALayeralpha?

alpha objective-c uibutton calayer ios

6
推荐指数
1
解决办法
2461
查看次数

带有纯色的UINavigationBar - ios 7

我正在使用UINavigationController它的默认值UINavigationBar,我试图关闭半透明效果,所以我可以在它上面有一个纯色.显然,它不适用于代码,但只需单击一下就可以使用故事板(很奇怪?!!)

我的代码:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:APP_DELEGATE.map];
[navController setToolbarHidden:YES];
[navController.navigationBar setTranslucent:NO];
[navController.navigationBar setBarTintColor:[UIColor turquoiseColor]];
//    [[UINavigationBar appearance] setBarTintColor:[UIColor turquoiseColor]]; | also doesn't work
Run Code Online (Sandbox Code Playgroud)

为什么不起作用?我该如何解决这个问题?谢谢.

objective-c uinavigationbar uinavigationcontroller ios uistoryboard

6
推荐指数
2
解决办法
5632
查看次数

Xcode6 beta SourceKitService错误

我正在使用Xcode 5.1.1运行最新的Mavenricks

我从苹果公司下载了Xcode 6 beta版,这条消息不断出现.因此,我几乎不能编写任何代码,因为禁用了代码完成.

有谁知道如何解决这一问题 ?

在此输入图像描述

swift xcode6

5
推荐指数
1
解决办法
2239
查看次数

Swift中的Checksum和XOR

我在Objective-C中对这些方法进行了研究.它们只是校验和而XORNSData

- (void)XOR:(NSMutableData *)inputData withKey:(NSData *)key
{
    unsigned char* inputByteData = (unsigned char*)[inputData mutableBytes];
    unsigned char* keyByteData   = (unsigned char*)[key bytes];
    for (int i = 0; i < [inputData length]; i++)
    {
        inputByteData[i] = inputByteData[i] ^ keyByteData[i % [key length]];
    }
}

- (Byte)checkSum:(NSMutableData *)data withLength:(Byte)dataLength
{
    Byte * dataByte = (Byte *)malloc(dataLength);
    memcpy(dataByte, [data bytes], dataLength);

    Byte result = 0;
    int count = 0;
    while (dataLength>0) {
        result += dataByte[count];
        dataLength--;
        count++;
    };
    result = result&0xff;
    return result&0xff; …
Run Code Online (Sandbox Code Playgroud)

bitwise-operators nsdata ios swift

5
推荐指数
2
解决办法
3274
查看次数

结合Alamofire和RxSwift

我有这个自定义的实现Alamofire:

protocol HTTPProtocol: class {
    typealias RequestType
    typealias RespondType
    func doRequest(requestData: RequestType) -> Self
    func completionHandler(block:(Result<RespondType, NSError>) -> Void) -> Self
}

//example of a request:
locationInfo
      //Make a request
    .doRequest(HTTPLocationInfo.RequestType(coordinate: $0))

      //Call back when request finished
    .completionHandler { result in
        switch result {
            case .Success(let info): self.locationInfoRequestSuccess(info)
            case .Failure(let error): self.locationInfoRequestFailed(error)
        }               
    }
Run Code Online (Sandbox Code Playgroud)

我想将MVVM和RxSwift应用到我的项目中.但是,我找不到合适的方法来做到这一点.

我想要实现的是a ViewModel和a ViewController可以做这些事情:

class ViewController {
    func googleMapDelegate(mapMoveToCoordinate: CLLocationCoordinate2D) {
        // Step 1: set new value on `viewModel.newCoordinate` and make a …
Run Code Online (Sandbox Code Playgroud)

mvvm ios swift rx-swift

5
推荐指数
2
解决办法
8225
查看次数

ios - UITableViewCell在滚动时显示错误的数据

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierNormal = @"CellNormal";
static NSString *CellIdentifierTracking = @"CellTracking";

switch (self.mapState) {
    case MapStateNormal:
    {
//            UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifierNormal];
//            if (cell == nil) {
            UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierNormal];
//            }
        [cell.textLabel setText:@"abc"];
        return cell;
    }
    case MapStateTracking:
    {
//            UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifierTracking];
//            if (cell == nil) {
            UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierTracking];
//            }
        if (indexPath.row == 0) {
            [cell.textLabel setText:@"Lorem ipsum"]; …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview ios

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

即使具有不同的单元格样式,UITableViewCell detailTextLabel也不会显示

这是我的代码:

- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_table setDelegate:self];
        [_table setDataSource:self];
        [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    }
    return _table;
}


- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    else

    [self configureCell:cell forRowAtIndexPath:indexPath];

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

问题是当我registerClass为我的桌子时,它假定我的细胞样式是UITableViewCellStyleDefault.所以这就是为什么detailTextLabel不出现.我测试了它.

注释掉该registerClass行不起作用,因为我没有任何CellIdentifier用途dequeueReusableCell.所以它会抛出一些例外.

如果我不使用dequeue,它可以工作,但这不是最好的做法.

AFAIK,表格单元格在初始化后无法改变其样式.那我该如何制作detailTextLabel秀呢?

objective-c uitableview ios

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

在Swift中为文字值设置关联对象

好吧,这可能是一个重复的问题.
我发现了一些像这样的问题: 有没有办法在Swift中设置相关对象?

但是,我想在Intswift中添加一个属性extension,上面链接中的这些答案不起作用.

这是我的代码:

import ObjectiveC
var xoAssociationKey: UInt8 = 0

extension NSData {

    var position: Int {
        get {
            return objc_getAssociatedObject(self, &xoAssociationKey) as Int
        }
        set {
            objc_setAssociatedObject(self, &xoAssociationKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }

    override convenience init() {
        self.init()
        position = 0
    }
}
Run Code Online (Sandbox Code Playgroud)

fatal error: unexpectedly found nil while unwrapping an Optional value每次访问都会得到position

仅供参考,我确实在Objective C中找到了这个错误的解决方案,我正在寻找一个快速的解决方案.如果您感兴趣,这是我在目标C中的代码:

static char PROPERTY_KEY;

@implementation NSData (Extension)
@dynamic position;
- (NSInteger)position {
    return  [objc_getAssociatedObject(self, &PROPERTY_KEY) …
Run Code Online (Sandbox Code Playgroud)

ios swift

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

用自定义运算符替换enum的rawValue属性

嗯,说实话,我不喜欢rawValue在访问enum价值时打电话.
enum几乎一直都这样使用,我认为调用.rawValue使我的代码不那么"可读":

enum FontSize: CGFloat {
    case Small = 12
    case Normal = 15
    case Large = 18
}
enum Example: String {
    case First = "First"
    case Second = "Second"
}
Run Code Online (Sandbox Code Playgroud)

所以,我正在尝试为enum"覆盖" 定义一个泛型运算符.rawValue.
我可以非一般地做.

postfix operator .. { }

postfix func .. (lhs: Example) -> String {
    return lhs.rawValue
}

postfix func .. (lhs: FontSize) -> CGFloat {
    return lhs.rawValue
}
Run Code Online (Sandbox Code Playgroud)

但是,我很懒,我想要一个通用的解决方案.写一个,全部工作.

有人可以帮我吗?谢谢.


更新:如果您希望增加/减少功能谁感兴趣这个问题,人们enum喜欢 …

ios swift

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

没有ARC的UITableViewCell标识符泛滥内存

我正在开发一个没有ARC的旧项目.它有很多错误,代码看起来很难看,我正在重写它.

快速浏览一下我的代码吧

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell = [self createCellWithInfo:[self.search objectAtIndex:indexPath.row]];
    return cell;
}

-(UITableViewCell *)createCellWithInfo:(NSDictionary *)info{

    UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“Cell”] autorelease];
    //set image for cell
    //set text for cell.textlabel
    //set text for cell.detailTextLabel
    //create an UIButton and add to cell.content view
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

重点在于这行代码 [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“Cell”] autorelease]

如果我@"Cell" …

objective-c uitableview ios automatic-ref-counting

0
推荐指数
1
解决办法
440
查看次数