Tag Archives: iOS

UITableView 离奇滚动到顶部的问题

你是否有过类似的经验,从一个TableView push到另一个视图后,然后pull back, 这时候有可能会发现tableView的contentOffset 已经滚动到顶部了,而且时有时无,所以也就没深入探讨下去了?

举个很简单的例子,现在有个附件位置聊天工具叫 陌陌, 我就喜欢在列表页面一直向下滑动找各种美女看PP,可是在看完美女资料返回列表后经常发现居然又滚回视图的顶部了,悲痛之余,又得话费很久时间重新滚动到下面,继续看美女。然后又被离奇的滚动到顶部。。。。

其实这个问题在 这篇文章 里提到过,没错还是 -viewDidUnload 导致的问题。首先我们来快速重现下这个bug

  1. 新建一个iOS项目,模板使用 Navigation-base App
  2. 在默认的tableViewController里
    1. - viewDidLoad 设置下 self.title=@”table”
    2. - numberOfRows 设置为 200 个cell
    3. 给每个cell添加点文字,当前的indexPath.row 的值就很好
    4. - didSelectRow 里随便push到个新的 ViewController
    5. - viewDidUnload 加个NSLog,方便跟踪消息
  3. 向下滚动些cell,然后点击进入第二个vc的视图
  4. 选择模拟器的菜单 Hardware->Simulate Memory warning , 你应该能在console里看到2行log,第二行log应该是来自于你的 -viewDidUnload  方法
  5. 返回tableView,你会发现bug重现,tableView的y偏移位已经变回0 !

其原因很容易解释,在接收到内存警报的通知后,非当前用户可见的视图控制器(vc)会接收到额外的通知,并将它的view给释放掉节约内存占用。然后当该vc的视图即将重新被用户可见时(比如被pullBack),vc会调用 -loadView , – viewDidLoad 这些方法重新将view创造出来, 但是偏移位却没有被保存下来,所以就发生这个Bug了。

写到这里应该已经知道如何解决这个问题了吧,增加一个变量记录来记录当前tableView.contentOffset.y 的数值,然后 – viewDidLoad 里面将tableview的偏移位给恢复即刻。

番外篇

iOS 中内存问题会导致各种奇怪的现象,在 – viewDidUnload被执行后会导致下次 – loadView, – viewDidLoad 被重新执行,所以一定要在 -viewDidUnload 中把其他retain的东西给释放掉。

每次通过模拟器的菜单来模拟内存警告比较麻烦,这里提供一个特殊的类来实现自动内存警报功能,将你的视图控制器从该类继承下来,这里以UIViewController为例子。当视图不可见时,该控制器便会自动接收到内存警告并释放掉自己的视图。这样子调试起来就方便了。有木有!!!

@interface MotherUIViewController : UIViewController
- (void)simulateMemoryWarning;
@end
@implementation MotherUIViewController
- (void)viewDidDisappear:(BOOL)animated{
	[super viewDidDisappear:animated];
	[self simulateMemoryWarning];
}
- (void)simulateMemoryWarning
{
#if TARGET_IPHONE_SIMULATOR
#ifdef DEBUG
	CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
#endif
#endif
}
@end

[iOS]UIActionSheet动态添加按钮

UIActionSheet 只提供了一个构造函数

- (id)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

如果需要动态的添加按钮,就必须使用另外个函数:

- (NSInteger)addButtonWithTitle:(NSString *)title;

但出现的诡异结果是新添加的内容均出现在 取消 按钮之下,非常讨厌。

解决方法直接看代码

//.....
    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"请选择埋掉的原因", @"reason to bury")
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];
    [sheet addButtonWithTitle:@"btn1"];
    [sheet addButtonWithTitle:@"btn1"];
    [sheet addButtonWithTitle:@"btn1"];
    [sheet addButtonWithTitle:NSLocalizedString(@"取消", @"cancel button title")];
    sheet.cancelButtonIndex = sheet.numberOfButtons - 1;
//....

10年之前

这里收录的是2010年前的各类大小开源项目。

虽然不够努力,但也留下了不少记忆。

iBook Page Curl Animation 翻页效果

好久没来RP掉的厉害,刚把wp升级到3.0,庆幸没什么异样。

最近很少花时间码代码,整理点资料加点RP

Steven 写的 Apple’s iBooks Dynamic Page Curl

Steven: I manage the CoreAnimation team at Apple. Are you interested in working for us?

注意文章的评论,以及另一个没使用private api的项目

http://github.com/brow/leaves

还有一篇

App Store-safe Page Curl animations by Ole Begemann

App Store 销售调查报告[2010-5]

见到cocoachina上dr_watson的分享

原文地址 http://techcrunch.com/2010/05/16/iphone-app-sales-exposed/

摘录其中一段

However, when the top 10% of the most successful apps are removed from the data set, the numbers skew much lower, giving a far better impression of what the iPhone industry looks like for most developers. In this scenario, the average sales were 11,625 total units, averaging 44 copies/day. Approximately 23% of apps sold less than 1000 units from launch (ranging from 12 to 370 days in the App Store). Further, 56% of apps sold less than or equal to 10,000 units, while 90% sold less than 100,000 units, with the remaining 10% achieving sales of 127,000 – 3,000,000 units.

now or never

1)

有些事,当你经历过挫折后,才想起人家曾经已告诫过.

有些事,明知道如何去做对,却终究延续现有的道路.

Read more »

WebView 小技巧

apple 把 webview 封装的实在太简单了.

留给开发者只有寥寥几个api,甚至连scrollview的基本操作都没法完成.

不过下面这个方法

stringByEvaluatingJavaScriptFromString:String

还是给了些希望.

搜索后发现果然javascript提供了一系列移动页面的操作

  • window.pageYOffset   获得当前页面的偏移
  • window.scrollTo(x,y) 将页面偏移到指定位置

这样我的需求就实现了.当然js能做的事情太多了,给webview换一套css也不是不可能的.

好像 Reeder 以及 便携百科ipad 就是这么做到的吧. 这两款软件都做得相当完美,体验上也无可挑剔.作者确实费了不少心血.

用心的产品才能引起共鸣.

A new approach to my career

谢谢.

再见.

嗯,时光飞逝,岁月如梭(冷..)

在上海的4个月后,我辞职了.

算上两份实习,目前我共做过4份工作,内容上分别是.

web 开发(Rails), web 开发(Java),  iphone 软件 , 跨平台游戏.

算是体验了不少东西,也认识了不少各行业的朋友.

但接下来不可再随性下去,控制自己的欲望,得制定些人生的目标了.

日复一日

月复一月

年复一年

这个月,开始挑战些极限的东西.

Apple 发疯了

Apple 发布了 os4.0

新的Game Center -> openFeint , Plus+ 等第三方平台怎么活下去。矛盾开始了

iAD -> 公开指责 google ,又一个敌人

以及 3.3.1

3.3.1 — Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).

这下 unity 和 即将来临的flash也被一棍打死了。

unity3d 论坛火爆讨论

Adobe 目前在twitter上的声明 :AdobeWe are looking into the new SDK language. We continue to develop Packager for iPhone OS which will debut in Flash #CS5

当然cocos2d 也赶紧来了个声明,哈哈不关我的事

Tutorial of kissXML(iPhone)

KissXML is a good approach for parsing xml data, and the x-path function make it more powerful.

Integrate With You Project And KissXML

  • Download source codes form here
  • Add all the files to your project (excluding DDXMLTesting)
  • Configure you project to work with libxml

click Project -> Edit Project Settings

You’ll be adding this to your compiler instructions

OTHER_LDFLAGS = -lxml2

HEADER_SEARCH_PATHS = /usr/include/libxml2

Using KissXML

Here is a quick demo to indicate you how it works.

For example, we need to get the SRC value of each media field from the target xml file.

<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">
<head>
</head>
<body>
<par dur="120000ms" >
<text region="Text" src="att000.txt" />
</par>
<par dur="120000ms" >
<text region="Text" src="att010.txt" />
</par>
<par dur="10000ms" >
<img region="Image" src="att020.jpg"/>
</par>
<par dur="120000ms" >
<text region="Text" src="att040.txt" />
</par>
<par dur="10000ms" >
<img region="Image" src="att120.gif"/>
</par>
</body>
</smil>

Here are the codes !

//hack to remove xmlns => avoid xpath search not works
 xmlStr = [xmlStr stringByReplacingOccurrencesOfString:@"xmlns" withString:@"noNSxml"];
 NSMutableArray* contents = [NSMutableArray array];
 NSError* error = nil;
 DDXMLDocument* xmlDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:&error];
 if (error) {
 NSLog(@"%@",[error localizedDescription]);
 return nil;
 }
 NSArray* resultNodes = nil;
 resultNodes = [xmlDoc nodesForXPath:@"//audio | //text | //image | //img" error:&error];
 if (error) {
 NSLog(@"%@",[error localizedDescription]);
 return nil;
 }
 for(DDXMLElement* resultElement in resultNodes)
 {
 NSString* name = [resultElement name];
 //audio , text or other media type
 NSString* fileName = [[resultElement attributeForName:@"src"] stringValue];
 // 0.txt
 }

Note, I replaced the “xmlns” inside the xml file, because it weird xpath would failed if namespace available at a XML file(it might a bug)

And at last, have fun!