objective-c - 自定义 UITableViewCells 可能在 iOS 6 中工作但在 iOS 5 中不起作用的原因有哪些?

标签 objective-c uitableview subclass reloaddata

我在尝试让表格 View 在我的 iPhone 上运行时遇到了很多麻烦。奇怪的是,它似乎在我的 iOS 模拟器上工作得很好(即,我可以向数组添加一个条目,并且该条目显示在我的 TableView 中)。但是,当我尝试在使用 iOS 设备时添加条目时,代码在 dequeueReusableCellWithIdentifier: 行上中断。我已经检查了大小写不一致、名称不一致,在自定义 UITableViewCell 子类中重新实现了 prepareForReuse,尝试使用 IBOutlets v. 标签在我的 UITableViewCell 子类中定义字段,也许还有其他一些东西,但没有一个工作了。

这个问题与我之前的问题无关:Debugging strategies when UITableView's cells don't load?

编程的困难部分总是知道要问哪个问题,所以如果事实证明我问了错误的问题,我深表歉意。

更新 6:问题是 iOS 5 上 UITableViewCell 的自定义布局

我使用 UITableViewCell 的子类和具有自定义布局的 UITableViewCell 进行了测试。使用具有 UITableViewCellStyleDefault 样式的 UITableViewCell 子类确实适用于 iOS 5 和 iOS 6 iPhone 模拟器。但是,使用具有自定义样式的通用 UITableViewCell 会在 iOS 5 上崩溃,但不会在 iOS 6 上崩溃。有趣的是,我在 UITableViewCell 的文档中没有看到自定义 UITableViewCellStyle 的声明...

更新 5:iOS 5 v.6 + 自定义 UITableViewCell 子类?

您好:今天继续测试,看来这是 iOS 5 和 6 如何处理自定义 UITableViewCell 子类之间的问题。还没有解决方案:(

更新 4:iOS 5 与 iOS 6?

所以我所能注意到的是,这似乎是 iOS 5 与 iOS 6 的问题。当使用行 GlassboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 在 iOS 6 上测试时 下面的代码有效。但是,该行和 GlassboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] 在 iOS 5 中都不起作用。有什么想法吗?通过将标识符更改为 protocell,我以某种方式让它只工作一次。

更新 3:我现在使用 GitHub!

这是相关的存储库:https://github.com/kenmhaggerty/Glassbox

更新 2:更多观察

所以我添加了 @synthesize tableView = _tableView 因为我在某处读到了可能有帮助的响应,但我现在意识到即使在运行时它也会阻止我的数据加载到 TableView 中在 iOS 模拟器上。注释掉该行代码会使代码返回到我上面描述的方式:它在 iOS 模拟器上工作得很好,但在 dequeueReusableCellWithIdentifier: 行上中断,没有指定错误,只是 Thread 1:断点1.1.

更新1:相关代码

GlassboxTableViewController.h

//
//  GlassboxTableViewController.h
//  Glassbox
//
//  Created by Ken M. Haggerty on 10/22/12.
//  Copyright (c) 2012 Ken M. Haggerty. All rights reserved.
//

#pragma mark - // NOTES (Public) //

#pragma mark - // IMPORTS (Public) //

#import <UIKit/UIKit.h>

#pragma mark - // PROTOCOLS //

//@protocol GlassboxTableViewDatasource <NSObject>
//@property (nonatomic, weak) NSMutableArray *arrayOfPlayers;
//@end

#pragma mark - // DEFINITIONS (Public) //

@interface GlassboxTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *arrayOfPlayers;
- (IBAction)addPlayer:(UIBarButtonItem *)sender;
//@property (nonatomic, strong) id <GlassboxTableViewDatasource> datasource;
@end

GlassboxTableViewController.m

//
//  GlassboxTableViewController.m
//  Glassbox
//
//  Created by Ken M. Haggerty on 10/22/12.
//  Copyright (c) 2012 Ken M. Haggerty. All rights reserved.
//

#pragma mark - // NOTES (Private) //

#pragma mark - // IMPORTS (Private) //

#import "GlassboxTableViewController.h"
#import "GlassboxCell.h"
#import "Player.h"
#import <MobileCoreServices/MobileCoreServices.h>

#pragma mark - // DEFINITIONS (Private) //

#define SIDEBAR_WIDTH_PERCENT 0.75

@interface GlassboxTableViewController () <UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
//@property (nonatomic, weak) IBOutlet UITableView *tableView;
- (void)setup;
@end

@implementation GlassboxTableViewController

#pragma mark - // SETTERS AND GETTERS //

@synthesize arrayOfPlayers = _arrayOfPlayers;
@synthesize tableView = _tableView;
//@synthesize datasource = _datasource;

- (void)setArrayOfPlayers:(NSMutableArray *)arrayOfPlayers
{
    _arrayOfPlayers = arrayOfPlayers;
}

- (NSMutableArray *)arrayOfPlayers
{
    if (!_arrayOfPlayers) _arrayOfPlayers = [[NSMutableArray alloc] init];
//    [_arrayOfPlayers addObject:[[Player alloc] initWithUsername:@"Ken H.:"]];
    return _arrayOfPlayers;
}

#pragma mark - // INITS AND LOADS //

- (void)setup
{
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    NSLog(@"[initWithStyle]");
    self = [super initWithStyle:style];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"[viewDidLoad]");
    [super viewDidLoad];
    [self setup];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width*SIDEBAR_WIDTH_PERCENT, self.view.frame.size.height)];
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - // PUBLIC FUNCTIONS //

- (IBAction)addPlayer:(UIBarButtonItem *)sender
{
    [self alertAddPlayer];
}

#pragma mark - // PRIVATE FUNCTIONS //

- (void)alertAddPlayer
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add New Player" message:@"Please type player name:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = 1;
    [alert show];
}

- (void)alertInvalidPlayer
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Name" message:@"Please type another name:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = 1;
    [alert show];
}

- (void)alertAddPhoto
{
    NSLog(@"[TEST] alertAddPhoto");
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        if ([mediaTypes containsObject:(NSString *)kUTTypeImage])
        {
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = self;
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerController.allowsEditing = YES;
//            imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//            imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
//            [self presentViewController:imagePickerController animated:YES completion:nil];
        imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
            [self presentModalViewController:imagePickerController animated:YES];
            return;
        }
    }
    NSLog(@"[TEST] No camera available");
    [self.tableView reloadData];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image)
    {
        [[self.arrayOfPlayers lastObject] setPhoto:[[UIImageView alloc] initWithImage:image]];
    }
    [self dismissImagePicker];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissImagePicker];
}

- (void)dismissImagePicker
{
//    [self dismissViewControllerAnimated:YES completion:^{
//        [self.tableView reloadData];
//    }];
    [self dismissModalViewControllerAnimated:YES];
    [self.tableView reloadData];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) NSLog(@"Cancel tapped");
    else
    {
        if (alertView.tag == 1)
        {
            if (buttonIndex == 1)
            {
                if ([[[alertView textFieldAtIndex:0] text] length] != 0)
                {
                    [self.arrayOfPlayers addObject:[[Player alloc] initWithUsername:[[alertView textFieldAtIndex:0] text]]];
                    [self alertAddPhoto];
                }
                else [self alertInvalidPlayer];
            }
        }
    }
}

#pragma mark - // PRIVATE FUNCTIONS (Miscellaneous) //

// TableView data source //

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//#warning Potentially incomplete method implementation.
//    // Return the number of sections.
//    return 0;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return self.datasource.arrayOfPlayers.count;
    return self.arrayOfPlayers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"New Cell";
//    GlassboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    GlassboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.name.text = [[self.arrayOfPlayers objectAtIndex:indexPath.row] username];
    cell.action.text = @"LOADED SUCCESSFULLY";
    cell.time.text = @"Just now";
    cell.photo = [[self.arrayOfPlayers objectAtIndex:indexPath.row] photo];

//    [((UILabel *)[cell viewWithTag:1]) setText:[[self.arrayOfPlayers objectAtIndex:indexPath.row] username]];
//    [((UILabel *)[cell viewWithTag:2]) setText:@"has been added."];
//    [((UILabel *)[cell viewWithTag:3]) setText:@"Just now"];
//    [((UIImageView *)[cell viewWithTag:4]) setImage:[[[self.arrayOfPlayers objectAtIndex:indexPath.row] photo] image]];

    [cell.contentView setFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, 120)];

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

// TableView delegate //

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

请告诉我是否应该发布更多内容。

最佳答案

(您的github项目无法编译(缺少Player.h/Player.m),因此很难重现该问题。)

但我注意到 MainStoryboard 文件中启用了“使用自动布局”。自动布局仅适用于 iOS 6 及更高版本,不适用于 iOS 5!

关于objective-c - 自定义 UITableViewCells 可能在 iOS 6 中工作但在 iOS 5 中不起作用的原因有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059985/

相关文章:

C++基础类

ios - CellForRowAtIndexPath 中给出的 UIButton 标记仅给出自定义 UITableViewCell 中的最后一个标记值

ios - UITableViewCells Nib 在滚动时显示默认值

iphone - 如何覆盖 UITableViewCell 中的 accessoryView 以更改其位置

objective-c - 我应该继承 NSMutableArray 类吗

c++ - auto之前的私有(private)类的实例

objective-c - 为什么 Cocoa 开发中没有动机传递指针?

ios - 更改 UISearchController 上的状态栏颜色

ios - 如果应用程序第二次运行,从 executeFetchRequest 返回的记录为空

ios - UITableViewCells 未出现在第二个选项卡中