搜尋此網誌

2012年9月9日 星期日

[xxxx]UILabe & UITableView&UITableViewCell問題集

1.怎么才能把UITableViewCell设为不可选中。
就像把按钮的enabled设为NO一样。全部鎖著cell.userInteractionEnabled = NO;
祇鎖一個cell.selectionStyle = UITableViewCellSelectionStyleNone;


2.如何讓uitableview固定
ANS:tableView.scrollEnabled

3. 如何換行

lblNeedSubscription = [[UILabel alloc] initWithFrame:frame];
[lblNeedSubscription setNumberOfLines:0];  // allows as many lines as needed
[lblNeedSubscription setText:@"To access content\ryou need to be a paid subscrib


表示不能換行且自動將自縮小
factLabel.numberOfLines = 1
factLabel.minimumFontSize = 8.;
factLabel.adjustsFontSizeToFitWidth = YES;
In this way your text's font size will reduce down to (for example) 8 trying to fit your text within the label.numberOfLines = 1 is mandatory.
For numberOfLines > 1 there is a method to figure out the size of final text through NSString's UIKit addition methods, for example:
CGSize lLabelSIze = [yourText sizeWithFont: factLabel.font forWidth:factLabel.frame.size.width lineBreakMode:factLabel.lineBreakMode];
After that you can just resize your label using resulting lLabelSIze, for example (assuming that you will change only label's height):
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSIze.height);

4.換顏色
label.textColor = [UIColor redColor];
5.給予不同cell不同高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//判斷每個row所需要高度,判斷條件與產生cell一樣
    if (!videoList) {
        return 64;
    }else if(indexPath.row<[videoList count]){
        return 64;
    }else{//顯示更多高度降低
        return 48;
    }
}



objective-c blocks

轉貼:http://blog.sina.com.cn/s/blog_67419c420100vl8b.html

 在ios4之后,引入了代码块的特性,在gcd中会经常的用到,所以决定好好的看看代码块文档,把这块总结一下。从头开始讲解代码块。

2012年9月7日 星期五

Blocks


轉貼:http://lldong.github.com/blog/2011/12/30/blocks/
从Mac OS X 10.6以及iOS 4开始,苹果在GCC和Clang编译器中为C语言引入了一个新扩展:Blocks,使得程序员可以在C、Objective-C、C++和Objective-C中使用闭包。Blocks有点像函数,但是它可以在其它函数或方法中进行声明和定义,同时它还是匿名的(匿名函数),并可以捕获其所在作用域中的变量(闭包特性)。

详解Objective-c Block应用


目前只有Mac 10.6 和iOS 4有支援。Block是由一堆可執行的程式組成,也可以稱做沒有名字的Function (Anonymous function)
详解Objective-c block应用是本文要介绍的内容,主要介绍的是Objective-c block应用,不多说,先来看详细内容。
AppleCObjective-CC++加上Block這個延申用法。目前只有Mac 10.6 和iOS 4有支援。Block是由一堆可執行的程式組成,也可以稱做沒有名字的Function (Anonymous function)。如果是Mac 10.6 或 iOS 4.0 之前的平台可以利用 http://code.google.com/p/plblocks/ 這個project得以支援Block語法。
Apple有一個叫做GCD(Grand Central Dispach)的新功能,用在同步處理(concurrency)的環境下有更好的效率。Block語法產生的動機就是來自於GCD,用Block包好一個工作量交給GCD,GCD有一個宏觀的視野可以來分配CPU,GPU,Memory的來下最好的決定。
Block 簡介
Block其實行為和Function很像,最大的差別是在可以存取同一個Scope的變數值。
Block 實體會長成這樣
^(傳入參數列) {行為主體};
Block實體開頭是"^",接著是由小括號所包起來的參數列(比如 int a, int b, float c),行為的主體由大括號包起來,專有名詞叫做block literal。行為主體可以用return回傳值,型別會被compiler自動辦識出來。如果沒有參數列要這樣寫(void)。
看個列子
  1. ^(int a) {return a*a;}; 

Objective-C block


Block memory

block 的内存管理,应该是最头疼的地方,就用这个来自WWDC的例子来解释一下吧。
image
当程序运行到这里时,stack 空间中有 shared 变量和 captured 变量。
这里可以看出,__block 变量开始是处于stack上的。

2012年9月6日 星期四

NSTimer


@interface
    NSTimer *autoTimer;
@implementation
// Start timer
    autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0)
        target:self 
        selector:@selector(autoTimerFired:) 
        userInfo:nil 
        repeats:YES];
// Stop timer:
    [autoTimer invalidate];
    autoTimer = nil;

監聽TextField change


.h

@property (strong, nonatomic) IBOutlet UITextField *account;
@property (strong, nonatomic) IBOutlet UITextField *password;

@property (strong, nonatomic) IBOutlet UITextField *displayAccount;
@property (strong, nonatomic) IBOutlet UITextField *displayPassword;



.m
- (void)viewDidLoad
{
//建立監聽事件


    [displayAccount addTarget:self action:@selector(onAccountChange) forControlEvents:UIControlEventEditingChanged];
    [displayPassword addTarget:self action:@selector(onPasswordChange) forControlEvents:UIControlEventEditingChanged];
}


-(void) onAccountChange{
    account.text=displayAccount.text;
}
-(void) onPasswordChange{
    password.text=displayPassword.text;
}

iOS开发 对话框UIActionSheet和UIAlertView用法


UIActionSheet和UIAlertView在iOS中,都是弹出一个对话框,用户必须点击上面的按钮后才能执行其他操作。
下图是UIActionSheet,对话框显示在底部:

iphone屏幕旋转、改变 视图尺寸 和 布局控制


如果  你  曾经  用  用户界面套件  开发过  桌面窗口应用程序(比如:Windows,Mac OS X,Linux,Qt  或者  GTK+),你  就应该了解到  在窗口的尺寸改变时  我们设计的用户界面  也要能  正确地  改变  自己的尺寸。在这个过程中  我们  要确定  窗口中  哪些肉眼可见的部件  要移动位置,哪些  要根据窗口的变化  改变  自己的尺寸。
有  一件事情  很明显:在iphone  和  ipad这种运行iOS的设备上  窗口  并不能  像桌面应用程序一样  允许  用户  改变。然而  这样的设备  可以感知  物理上的转动,从而  允许  应用程序  以竖屏模式  或者  横屏模式  运行。既然  iOS设备  有  这样的能力,那么  我们  很有  必要  使  我们的应用程序用户界面布局  根据  设备的旋转  而  改变。
接下来  我们  要了解  设备转动  和  用户界面重新布局的机制。

判斷是哪個class


// DO NOT DO THIS!
if ([myArray isKindOfClass:[NSMutableArray class]])
{
    // Modify the object
}

2012年9月4日 星期二

如何判斷是哪個alert


方法一
@property (nonatomic, strong) UIAlertView *passwordAlert;
@property (nonatomic, strong) UIAlertView *warningAlert;

passwordAlert = [[UIAlertView alloc] initWithTitle:@"Password" message:@"Enter password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
passwordAlert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[passwordAlert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   if (alertView == passwordAlert) {
      // handle anything for password button indexes here
   }
   if (alertView == warningAlert)_ {
      // handle anything for warning button indexes here
   }
}
方法二

使用storyboard實現view switch,和參數傳遞


轉載:http://blog.csdn.net/mad1989/article/details/7919504 

由于最近才接触到IOS,苹果已经建议storyboard来搭建所有界面了,于是我也追随时尚,直接开始使用storyboard。(不料在涉及到页面跳转的时候,遇到的问题是:点击后没有任何反应)众所周知,在storyboard中,界面的跳转是通过segue来实现的,利用它,省去了方法内写入跳转的代码。

在app中開啓設定(settings)的方法



[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=Network"]]


2012年9月2日 星期日

Resize a UIImage the right way

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

iPhone开发技巧之网络篇(5)--- 使用libcurl连接https服务器




  • 博主:易飞扬
  • 原文链接 : http://www.yifeiyang.net/iphone-web-development-skills-of-the-article-5-https-server-using-libcurl-to-connect/
  • 转载请保留上面文字。


  • iPhone开发技巧之网络篇(4)--- 确认网络环境 3G/WIFI



  • 博主:易飞扬
  • 原文链接 : http://www.yifeiyang.net/iphone-web-development-skills-of-the-article-4-make-sure-the-network-environment-3gwifi/
  • 转载请保留上面文字。

  • iPhone开发技巧之网络篇(4)--- 确认网络环境 3G/WIFI
    开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。

    iPhone开发技巧之网络篇(3)--- 使用NSOperation建立多任务网络连接



  • 博主:易飞扬
  • 原文链接 : http://www.yifeiyang.net/iphone-web-development-skills-of-the-articles-3-use-a-multi-task-network-connection-nsoperation/
  • 转载请保留上面文字。

  • iPhone开发技巧之网络篇(3)--- 使用NSOperation建立多任务网络连接
    在网络应用程序中,经常需要多任务连接来提高程序的性能。比如多任务下载,多任务HTTP请求等,即线程控制模型中的工作群模型。使用 NSOperation 可以很容易实现这个功能。下面就以使用NSOperation处理并行的HTTP请求为例子,说明其用法。
    首先准备一个 NSOperation 的子类,用于处理 HTTP 请求。
    1
    2
    3
    4
    5
    6
    7
    8
    
    @interface RequestOperation : NSOperation {
      NSURLRequest*  _request;
      NSMutableData* _data;
    }
    
    - (id)initWithRequest:(NSURLRequest *)request;
    
    @end
    

    iPhone开发技巧之网络篇(2)--- Web服务



  • 博主:易飞扬
  • 原文链接 : http://www.yifeiyang.net/iphone-web-development-techniques-of-the-chapter-2-web-service/
  • 转载请保留上面文字。

  • iPhone开发技巧之网络篇(2)--- Web服务
    说到XML不得不提WEB应用中最常见的几种通讯规范:SOAP,XML-RPC,REST,WSDL,JSON等,他们都是基于XML协定的。在这里介绍几种处理web应用中可以利用的程序库:
    现在云计算技术很火,无论是类似 Google App Engine 的 PAAS 还是 Amazon EC2 的 IAAS 服务或者是类似 Twitter 的 SAAS。不可避免的都需要与 XML 打交道。所以掌握了这个标准,开发网络应用就不怕了。
    关于这些协议的具体意义这里就不详述了,可查阅相关文档。这里只介绍一些封装好的类库,以便于开发。

    iPhone开发技巧之网络篇(1)— 解析XML




  • 博主:易飞扬
  • 原文链接 : http://www.yifeiyang.net/iphone-web-development-techniques-of-the-chapter-1-parsing-xml/
  • 转载请保留上面文字。


  • iPhone开发技巧之网络篇(1)--- 解析XML