(一)关于UITableView
1.任意设置Cell选中状态的背景色:
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor orangeColor];
self.selectedBackgroundView = bgView;
[bgView release];
该方法设置的是纯色, 也可以使用任何图片,把selectedBackgroundView设成UIImageView。
除了上面的方法,前几天发现了一个新方法:重写UITableViewCell 的
setSelected:
animated:方法
- (
void)setSelected:(
BOOL)selected animated:(
BOOL)animated
{
[ super setSelected:selected animated:animated];
if (selected) {
self .backgroundColor = RGB ( 224 , 152 , 40 );
}
else {
self .backgroundColor = [UIColor clearColor ];
}
}
{
[ super setSelected:selected animated:animated];
if (selected) {
self .backgroundColor = RGB ( 224 , 152 , 40 );
}
else {
self .backgroundColor = [UIColor clearColor ];
}
}
2.如果Table中有控件,这里以switch为例(适合其它可修改值的各种控件),要在switch的UIControlEventValueChanged事件的处理方法里把值记录下来。以下方法是不可取的:在执行的最后把所有cell遍历一遍,处理各控件的值。因为没显示出来的cell,是取不到的,当然也就取不到该cell里的控件。所以正确的做法是,在控件可见时,如果值变了,立即处理。当然,如果你的Cell少,不会出现隐藏的情况就随便了。
3.方法flashScrollIndicators:这个很有用,闪一下滚动条,暗示是否有可滚动的内容。可以在ViewDidAppear或[table reload]之后调用。
4.点击Cell中的按钮时,如何取所在的Cell:
-(void)OnTouchBtnInCell:(UIButton *)btn {
CGPoint point = btn.center;
point = [table convertPoint:point fromView:btn.superview];
NSIndexPath* indexpath = [table indexPathForRowAtPoint:point];
UITableViewCell *cell = [table cellForRowAtIndexPath:indexpath];
...
//也可以通过一路取btn的父窗口取到cell,但如果cell下通过好几层subview才到btn,就要取好几次 superview,所以我用上面的方法,比较通用。这种方法也适用于其它控件。
}
(二)设置线宽,如果是retina屏,lineWidth设为1,实际显示的宽度是2个像素,这里进行一下处理:
#define SETLINEWIDTH(ctx,w) CGContextSetLineWidth(ctx, w/[UIScreen mainScreen].scale)
(三)_cmd:表示该方法的selector,可以赋值给SEL类型的变量,可以做为参数传递。
例如一个显示消息的方法:
-(void)ShowNotifyWithString:(NSString *)notifyString fromMethod:(SEL) originalMethod;
originalMethod就是调用这个方法的selector。
调用:
NSString *stmp = @"test";
[self ShowNotifyWithString:stmp fromMethod:_cmd];
如何记录当前方法名称:
NSLog(NSStringFromSelector(_cmd));
(四)在CGContext中输出汉字:CGContextShowTextAtPoint是不支持汉字的,需要用NSString的drawAtPoint或drawInRect方法
(五)一个不停震动的方法:
// 定义一个回调函数,震动结束时再次发出震动
void MyAudioServicesSystemSoundCompletionProc (SystemSoundID ssID,void *clientData)
{
BOOL* iShouldKeepBuzzing = clientData;
if (*iShouldKeepBuzzing) { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
//Unregister, so we don't get called again...
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
}
}
以下为调用的代码:
BOOL iShouldKeepBuzzing = YES;
AudioServicesAddSystemSoundCompletion (
kSystemSoundID_Vibrate,
NULL,
NULL,
MyAudioServicesSystemSoundCompletionProc,
&iShouldKeepBuzzing );
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
(六)关于更新,iPhone自动保存document中的内容,如果你把文件放在document中,以后开发又改了这个文件的内容或格式,那更新之后运行很可能出错。解决的办法是,配置文件放bundle里,或者改个文件名。每次更新前都要从App store 下载旧版本,运行一段一时间后,再此基础上编译新版,运行不出错才能上传
(七)初学者或者不小心容易犯的错误:在dealloc里要调用[super dealloc],千万不要调用[super release]
(八)需要调试的类最好重写description,输出重要变量的值,因为调试窗口variableView有时候变量值显示不出来。
(九)去掉app图标的发光效果:info.plist里增加Icon already includes gloss effects,值设为YES
(十)写代码时字符串太长 怎么换行:NSString *string = @"ABCDEFGHIJKL" \
"MNOPQRSTUVsWXYZ";
"MNOPQRSTUVsWXYZ";
(十一)UIImage:stretchableImageWithLeftCapWidth:topCapHeight: 有时图片模糊(blur)的原因:像素没有和device pixel对齐.使用instrument 的Core Animation可以检测这个,勾选"color misaligned images",如果图片显示为红紫色,就是没有对齐
(十二)UIPopoverController如果是用presentPopoverFromBarButtonItem显示的,设备旋转时,popover可以自动调整位置;如果是用presentPopoverFromRect
显示的, 需要present again
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[aPopover presentPopoverFromRect:targetRect.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}