iPhone開發問題彙總

 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"];
}




[待續... ...]
相關文章
相關標籤/搜索