汇率换算项目总结

2017-08-31  本文已影响38人  落夏简叶

汇率换算问题集合

afn有时候请求接口会有问题

接口没错

  1. Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.

    (企图在 写入事务 外修改对象)[http://blog.csdn.net/qq_31810357/article/details/52088268];

  2. 修改UITableView 右侧索引栏的背景颜色和字体颜色

//索引栏底色
    tableView.sectionIndexBackgroundColor = [UIColor blackColor];

//索引字体颜色
    tableView.sectionIndexColor = [UIColor grayColor];
  1. 取消cell的选中状态
preferCell.selectionStyle = UITableViewCellSelectionStyleNone;
  1. 根据十六进制设置颜色
#define UIColorFromRGBA(rgbValue, alphaValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(float)alphaValue]
  1. 设置cell的属性的时候要等cell存在再设置,要不然会无效
//在注释处设置取消选中状态没用
static NSString *defaultCell = @"defaultCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultCell];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCell];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
  1. 计算cell的汇率,是总的基于一个接口,接口中包含了所有货币相对于USD的转换,所以我计算只用请求一次接口,而不是换算一个请求一个,大大减轻了对网络的依赖。

  2. 所有关于cell的设置都用model来设置,要不然会有重用的问题,很麻烦,(包括cell文字的颜色)。

  3. 接口以美元为基准货币的很多数据

https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json
  1. 最后一行有一个添加的功能,因为我是在sb里面拖拽的,但是,在sb里面为tableView设置footerView不是那么方便,所以,我采用了两个不同类型的cell做的。这样就是要判断,是不是最后一行。来显示不同的cell。这样有一个问题就是,我的tableView要有删除的功能,所以,这样还要再次判断cell是正常的cell还是添加的cell。感觉如果食用footerView会简单一些。

  2. Realm数据库修改数据,有两种方式,一是通过主键来修改。二,当没设置主键的时候,就从数据库中拿到对应要修改的数据,把它的属性一个个的重新赋值。

  3. Realm数据库里面取数据,有时不是按添加顺序取出,如果要拿到有一定顺序的数据,一定要排序!!!(这里被坑死了)

//这样取不一定是你想要的顺序
[currencyPreferenceModel allObjects]

//如果要有一定顺序的数据,则根据属性排序,如 indexPathRow 是 currencyPreferenceModel 中的属性
[[currencyPreferenceModel allObjects] sortedResultsUsingKeyPath:@"indexPathRow" ascending:YES]
  1. Realm数据搜索数据,根据多个条件搜索,用OR连接,如
NSPredicate *countyPredicate = [NSPredicate predicateWithFormat:@"countryName CONTAINS[cd] %@ OR currencyCode CONTAINS[cd] %@ OR currencyName CONTAINS[cd] %@",text,text,text];
self.searchResultArr = [self.defaultArray objectsWithPredicate:countyPredicate];
  1. 关于UITableViewCell的一些属性设置
//设置cell后面又个勾勾
cell.accessoryType = UITableViewCellAccessoryCheckmark;

//默认勾勾好像是蓝色的,想改变勾勾(cell.accessoryType)的颜色
cell.tintColor = [UIColor whiteColor];

//不想要cell的背景
cell.backgroundColor = [UIColor clearColor];

  1. 默认tableView的section的title只能设置title的内容,想要修改title的大小,颜色啥的,就在下面的方法该。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle==nil) {
        return nil;
    }
    
    // Create label with section title
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(12, 0, tableView.bounds.size.width, 50);
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = UIColorFromRGB(0xa2a2a2);
    label.font = [UIFont fontWithName:@"PingFangSC-light" size:15];
    label.text = sectionTitle;
    
    // Create header view and add label as a subview
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)];
    [sectionView setBackgroundColor:UIColorFromRGBA(0x00112c, 0.7)];
    [sectionView addSubview:label];
    return sectionView;
}

//设置了自定义的sectionView的高度,不要忘记在下面的方法也设置一下,小心错位啦
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}


UITextFiled 使用

  1. 点击textFiled的时候不让系统的键盘弹出。
//在textField开始编辑的时候
self.inputView = [[UIView alloc] initWithFrame:CGRectZero];
  1. 让textFile点击之后弹出自己想要的输入框
//自定义的一个view
self.inputView = [XXCalculator calculator];
这里要注意,这个自定义的view最好就是一个简简单单的view,我开始把这个view加到一个父view的时候(之前是为了封装这个view)就一直报错:“<UICompatibilityInputViewController: 0x14f9c6850> should have parent view controller: xxx but requested parent is:<UIInputWindowController: xxx>'”
  1. 很奇怪一点,当换成自己的输入view之后,不再响应editChange方法,不过editBegin方法还是可以响应。

  2. 当tableView中有textFiled的时候,此时键盘遮挡了textFiled,可以使用以下方法(让tableView的高度整体上移)

- (void)setTXListener {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    float height = keyboardRect.size.height;
    
    self.setTable.contentOffset = CGPointMake(0, height);
}


- (void)keyboardWillHide:(NSNotification *)aNotification{
    
    self.setTable.contentOffset = CGPointMake(0, 0);
}
  1. 改变textFiled的placeholder的颜色和大小,用sb的话,可以在sb的inspector中的User Defined Runtime Attributes 中通过keyPath的方式添加属性 参考下图
修改textFiled的placeholder属性.png
//代码设置
textField.placeholder = @"手机号码"; 
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
上一篇 下一篇

猜你喜欢

热点阅读