小编Lau*_*ell的帖子

获取UITableView以滚动到所选的UITextField并避免被键盘隐藏

我有一个(不是a )UITextField表格视图.如果表格视图位于a上,则表格将自动滚动到正在编辑的页面,以防止它被键盘隐藏.但事实并非如此.UIViewControllerUITableViewControllerUITableViewControllertextFieldUIViewController

我已经尝试了几天阅读多种方法来尝试实现这一点,我无法让它工作.实际滚动的最接近的是:

-(void) textFieldDidBeginEditing:(UITextField *)textField {

// SUPPOSEDLY Scroll to the current text field

CGRect textFieldRect = [textField frame];
[self.wordsTableView scrollRectToVisible:textFieldRect animated:YES];

}
Run Code Online (Sandbox Code Playgroud)

但是,这只会将表格滚动到最顶行.看似简单的任务是几天的挫败感.

我使用以下来构造tableView单元格:

- (UITableViewCell *)tableView:(UITableView *)aTableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:identifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryNone;

        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 10, 130, 25)]; …
Run Code Online (Sandbox Code Playgroud)

keyboard xcode uitableview uitextfield

46
推荐指数
4
解决办法
6万
查看次数

如何隐藏/显示导航栏中的右键

我需要在导航栏中隐藏右键,然后在用户选择一些选项后取消隐藏它.

不幸的是,以下不起作用:

NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES;  // FOO CODE
Run Code Online (Sandbox Code Playgroud)

有办法吗?

objective-c uibarbuttonitem rightbarbuttonitem ios

38
推荐指数
7
解决办法
6万
查看次数

NSUserDefaults的最佳实践同步

我使用的是[[NSUserDefaults standardUserDefaults] synchronize]每当我写什么plist中的时间.那有点矫枉过正吗?或者这样做有不良影响吗?

iphone xcode nsuserdefaults

37
推荐指数
2
解决办法
1万
查看次数

计算SQLite数据库中的行数

我正在尝试以下代码来计算我的SQLite数据库表中的行数,但它会引发异常.这是一种更简单的方法吗?

- (void) countRecords {
    int rows = 0;
    @try {
        NSString *dbPath = [self getDBPath];

        if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

            NSString *strSQL;
            strSQL = @"SELECT COUNT(*) FROM MYTABLE";
            const char *sql = (const char *) [strSQL UTF8String];
            sqlite3_stmt *stmt;

            if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) == SQLITE_OK) {

                // THIS IS WHERE IT FAILS:
                if (SQLITE_DONE!=sqlite3_step(stmt) ) {

                    NSAssert1(0,@"Error when counting rows  %s",sqlite3_errmsg(database));

                } else {
                    rows = sqlite3_column_int(stmt, 0);
                    NSLog(@"SQLite Rows: %i", rows);
                }
                sqlite3_finalize(stmt);
            } …
Run Code Online (Sandbox Code Playgroud)

sqlite objective-c count

11
推荐指数
2
解决办法
2万
查看次数

如何使用UITableViewCellAccessoryCheckmark取消选中所有行

我有一个UITableView包含复选框的每一行使用UITableViewCellAccessoryCheckmark.我无法弄清楚如何使用该didSelectRowAtIndexPath方法取消选中所有复选框.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  

    UITableViewCell *oldCell;

    int count = [self.myTableRowNamesArray count];

    for (NSUInteger i = 0; i < count; ++i) {                                
        // Uncheck all checkboxes
        // OF COURSE THIS DOESN'T WORK
        // BECAUSE i IS AN INTEGER AND INDEXPATH IS A POINTER
        FOO: oldCell = [myTableView cellForRowAtIndexPath:(int)i];
        // GOOD CODE:
        oldCell = [penanceOptionsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Run Code Online (Sandbox Code Playgroud)

iphone xcode checkmark

10
推荐指数
4
解决办法
2万
查看次数

使用可达性的大内存泄漏

使用设备上的仪器,它在我的应用程序中使用Apple的Reachability 2.0代码检测到3.50 KB的内存泄漏.泄漏的对象是GeneralBlock-3584.泄漏工具指向以下代码:

- (BOOL) startNotifer
{
    BOOL retVal = NO;
    SCNetworkReachabilityContext    context = {0, self, NULL, NULL, NULL};
    if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context))
    {
        // THIS IS LINE OF CODE WHERE THE LEAK OCCURS:
        if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
        {
            retVal = YES;
        }
    }
    return retVal;
}
Run Code Online (Sandbox Code Playgroud)

我几乎直接使用Apple示例代码中的Reachability示例,所以我无法弄清楚为什么会发生这种情况或我如何修复它.

memory iphone xcode memory-leaks reachability

7
推荐指数
1
解决办法
2053
查看次数

在特定位置将对象从一个阵列添加到另一个阵列

我有两个Mutable Arrays,firstArray和secondArray.两者都填充了对象.我想在firstArray中的特定点(不是在结尾而不是在开头)将secondArray中的对象添加到firstArray.有没有办法做到这一点?目前我只使用这行代码:

[self.firstArray addObjectsFromArray:secondArray];
Run Code Online (Sandbox Code Playgroud)

我想要的是FOO CODE:self.firstArray addObjectFromArray AT SPECIFIC POINT X:secondArray,specificpointX)

任何帮助表示赞赏!

objective-c nsmutablearray

5
推荐指数
1
解决办法
8173
查看次数

从CoreData获取特定的随机行数

我正在使用下面的代码使用符合搜索条件的CoreData获取所有行的查询集:itemType = 1.但我需要做的是从数据中获取特定数量的Random行.例如,我不需要检索列名为dataType = 1的所有100行数据,而是需要随机获取25行dataType = 1.我希望有相对无痛的解决方案.任何帮助表示赞赏.LQ

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:[NSEntityDescription entityForName:@"MyAppName" 
                    inManagedObjectContext:[self managedObjectContext]]];

NSError *error = nil;                                           
NSPredicate *predicate;
NSArray *fetchResults;
predicate = [NSPredicate predicateWithFormat:@"(itemType = %i)", 1];            
[request setPredicate:predicate];
fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

if (!fetchResults) {
        // NSLog(@"no fetch results error %@", error);
}

self.mutableArrayName = [NSMutableArray arrayWithArray:fetchResults];
[request release];
Run Code Online (Sandbox Code Playgroud)

iphone xcode predicate core-data fetch

4
推荐指数
1
解决办法
2374
查看次数

Modal View的NavigationController?

我正在使用导航控制器.我的RootViewController推送了许多视图,但它也提供了一个模态视图.该模态视图提供了一个tableView.所以我想要做的就是弄清楚我可以将导航控制器推到模态视图中,然后在该模态视图中使用它来推送带有tableView的视图?或者除此之外,有没有办法从模态视图实现第二个导航控制器?

真正的问题是我想使用从右到左过渡的tableView来呈现视图,这当然不适用于模态视图.

我有这个代码,SORT OF提供了导航控制器使用的左转换权限:

NewViewController *newViewController = [[NewViewController alloc] init];
[self presentModalViewController:newViewController animated:NO];

CGSize theSize = CGSizeMake(320, 460);
newViewController.view.frame = CGRectMake(0 + theSize.width, 0 + 20, theSize.width, theSize.height);
[UIView beginAnimations:@"animationID" context:NULL];
[UIView setAnimationDuration:0.5];
newViewController.view.frame = CGRectMake(0, 0 + 20, 320, 460);
[UIView commitAnimations];
[newViewController release];
Run Code Online (Sandbox Code Playgroud)

问题是OldViewController(调用NewViewController的那个)立即消失,因此NewViewController在空白屏幕上转换,而不是覆盖OldViewController.

iphone xcode uinavigationcontroller

3
推荐指数
1
解决办法
3831
查看次数

内存泄漏在NSConcreteMapTable中使用NSXMLParser

我正在使用NSXMLParser,我得到一个指向NSConcreteMapTable的内存泄漏,无论是什么:

在此输入图像描述

从我的AppDelegate.m调用解析器时,在这行代码中发生泄漏:

在此输入图像描述

我找了一个解决方案,看不出我做错了什么.这是我的代码.任何帮助是极大的赞赏.LQ

// * * * XMLParser.h * * *  

#import <Foundation/Foundation.h>

@protocol NSXMLParserDelegate;

@interface XMLParser : NSObject 
<NSXMLParserDelegate>
{
    NSMutableArray  *xmlArray;
    BOOL        storingCharacters;
    float       xmlDataVersion;
}

@property (nonatomic, retain) NSMutableArray *xmlArray;
@property (nonatomic)  BOOL storingCharacters;
@property (nonatomic, assign) float xmlDataVersion;

-(BOOL)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error;

@end

// * * * XMLParser.m * * *

#import "XMLParser.h"

@implementation XMLParser

@synthesize xmlArray;
@synthesize storingCharacters;
@synthesize xmlDataVersion;

- (BOOL)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error {

    BOOL result = YES;

    if (xmlArray == nil) { …
Run Code Online (Sandbox Code Playgroud)

xcode memory-leaks nsxmlparser

3
推荐指数
1
解决办法
1136
查看次数

读取UITableView Section Title的字符串值

我想将UITableView的节标题推送到另一个视图控制器的节标题.但我无法找到一种方法来阅读现有的部分标题.现有的节标题是动态构造的,我宁愿重复使用它而不是重新构建它.

 if (indexPath.section == 0) {

      SecondViewController *secondViewController = [[SecondViewController alloc] init];
      secondViewController.strValueHolder = FOO_section.sectionTitle; // FOO Code
      [[self navigationController] pushViewController:secondViewController animated:YES];
      [secondViewController release];

 }
Run Code Online (Sandbox Code Playgroud)

xcode uitableview sectionheader

2
推荐指数
1
解决办法
3021
查看次数

检查NSUserDefault字符串中的nil

我知道这似乎应该这么简单......

我从NSUserDefaults中提取字符串值:

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
 NSString *strFirstName = [userDefaults stringForKey:kFirstPersonName];
Run Code Online (Sandbox Code Playgroud)

这给了我"指针和整数之间的比较"错误:

 if (!strFirstName == nil) {    
      self.firstPersonName.text = strFirstName;
 }
Run Code Online (Sandbox Code Playgroud)

我要做的是从用户默认值中提取一个值.如果没有,请不要使用它.如果它有一个字符串值,请使用它.

任何帮助表示赞赏!LQ

null xcode nsuserdefaults

0
推荐指数
1
解决办法
3893
查看次数