早上来公司,忘带macbook的电源线,借了一台黑苹果,继续开始。
在新的环境下复习旧知识,重写昨天代码,一点懒都不能偷,反倒发现了新问题。
1,[retain]属性对象在获取使用后,是否需要释放。
不需要。[retain]影响的是赋值过程。记得有个总结叫:通过alloc,new,copy等方式获取的指针,在当前上下文中需要释放,否则不需要。
2,自定义UITableViewCell中添加UIButton的问题。
从《iPhone开发基础教程》第八章的例子照猫画虎,能将UILabel添加到UITableViewCell中并正确显示text,但换做UIButton就是不行,代码:
if(cell == nil){
创建UITableViewCell类型的cell;
UIButton * button = [[UIButton alloc] initWithFram:rect];
button.tag = xxxTag;
[cell.contentView addSubview: button];
[button release];
}
UIButton * btn = [cell viewWithTag:xxxTag];
[btn setTitle:@”haha” forState:UIControlStateNormal];
可就是显示不出来。
后来在创建UIButton处,改为autorelease方法就可以显示了:
UIButton * button = [UIButton buttonWithStyle:….];
当然不要忘了去掉上面的[button release];
大概花了四个小时。
3,往UINavigationBar添加右侧按钮。
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Red" style:UIBarButtonItemStylePlain target:self action:@selector(goRed)] autorelease];
这行代码从这里取得:
http://cncc.bingj.com/cache.aspx?q=UINavigationbar+%E5%8F%B3%E4%BE%A7%E6%8C%89%E9%92%AE&d=4528472243831718&mkt=zh-CN&setlang=zh-CN&w=5eae60fe,f0ed679d
只是一开始不知道self指的是谁,在delegate接口中狂试总是不行。后来恍然大悟,将代码放到viewController中顺利调通。
这两个问题今晚复盘。