您的位置:首页 > 移动开发 > IOS开发

iOS-iphone开发中的 一些小技巧

2014-11-15 14:03 316 查看
iPhone开发问题是本文要介绍的内容,主要是来讲述字啊iphone开发过程中遇到的一些BUG,如何来解决,来看详细内容一一解决方案。

Q:[让tableview滚动到顶端] 从另一个view进入到一个tableview时,总是会自动滚动到先前的滚动条位置,我想让它每次进入这个tableview时,都滚动回最顶端,应该用哪个消息呢?

A: 方法一:使用 scrollToRowAtIndexPath

方法二:

- (void)scrollToTop {  

        [self.tableView setContentOffset:CGPointMake(0,0) animated:YES];                  

}  

- (void)scrollToBottom {  

        NSUInteger sectionCount = [self.tableView numberOfSections];
 

        if (sectionCount) {  

                NSUInteger rowCount = [self.tableView numberOfRowsInSection:0];
 

                if (rowCount) {  

                        NSUInteger ii[2] = {0, rowCount - 1};  

                        NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:ii length:2];
 

                        [self.tableView scrollToRowAtIndexPath:indexPath   

                         atScrollPosition:UITableViewScrollPositionBottom animated:YES];  

                }  

        }          



方法三:

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] animated:YES   

 scrollPosition:UITableViewScrollPositionMiddle]; 

首先使用selectRowAtIndexes: 选择行数,滚动的话tableview的superview时scrollview,scrollview可以滚动到某个position 那么就要计算这个position position = table row height * index,就得到滚动的位置了。

Q:在使用SLQite3调用sqlite3_bind_text函数时需要使用char *类型的参数,在sqlite3_column_text函数中需要使用char *类型的返回值,如何将字符串对象在NSString和Char *之间进行转换?

A:

将NSString转换成char *:[NSString UTF8String]

将char *转换成NSString:[NSString stringWithUTF8String:]

例如:

//=======NSString to char *==============  

 NSString *updateSign = @"AAAA";  

 sqlite3_bind_text(statement, 1, [updateSign UTF8String], -1, NULL);  

   

//=========char * to NSString============
 

 columnName.text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]; 

Q:如何解决在iPhone程序开发中常遇到“unrecognized selector sent to instance”的问题?

A:造成该问题的大部分原因是对象被提前release了,在不希望它release的情况下,指针还在,对象已经不在了。主要是因为init初始化函数中,没有对属性使用self.[属性]=xxxx的方式赋值,而是直接对属性所对应的私有变量进行赋值,导致属性对象没有retain而提前释放。解决方法,使用self.[属性]=xxxx语句对属性赋值即可。

Q:我想计算两个NSDate的数据相差几天几个小时几分几秒怎么办阿?

A:

NSTimeInterval      

time = [date1 timeIntervalSinceDate:date2]; 

time是date1和date2的秒间隔,大于零说明date1比date2晚,反之。。。。要得到几天几分几秒的,算算就出来了。

Q:怎么实现一个登录页面,在登录成功后跳转到另一个页面(我想实现先是一个登录界面点击一个登录按钮载跳转到UITabBarController界面怎样处理啊)?

A:可以尝试下面的方法:

1,在MainWindow.xib里放入LoginViewController和UITabBarController。

2,Delegate里application加入下记代码。

[window addSubview:tabBarController];  

 [window addSubview:loginViewController]; 

3,Login成功后,在LoginViewController里加入下记代码。

[self.view removeFromSuperview]; 

Q:iPhone中如何实现类似于Timer的定时操作?

A:类似下面代码实现:

timer = [NSTimer scheduledTimerWithTimeInterval:(3) target:self selector:@selector (onTimer:) userInfo:nil repeats:YES];
 

   

- (void)onTimer:(NSTimer*)timer {  

         //处理  

              ......  

 } 

Q:UITableViewCell 里 有个 UITextField当点击UITextField时会出现软键盘,为了返回UITextField的值,我在valueChanged事件绑定了 rootViewController 的-(IBAction) textAction : (id) sender;可是我同时需要知道该Cell 的 indexPath.row 该怎么做?

A:有两种方法:

方法1

先获取UITextField所在的Cell.

NSIndexPath *path =    [tableView indexPathForCell:    
 

(UITableViewCell *) [ (UITextField *)sender superview] ]; 

方法2

首先,在table loadview 制造cell的时候在cell.tag和textField.tag 设个值

tmpcell.tag = 3;
 

tmpcell.textField.tag = 3; 

然后事件启动的时候这样

- (IBAction)textAction:(id)sender  

 {  

         NSInteger tag = [sender tag];
 

         NSIndexPath *indexPath = [self.tableView indexPathForCell: (UITableViewCell *)[self.tableView viewWithTag:tag]];
 

         [[[rawElementsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] setValue:[sender text] forKey:@"value"];  

 } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: