2

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

这个问题与我之前的问题切线相关:当 UITableView 的单元格不加载时调试策略?

编程的难点在于始终知道要问哪个问题,所以如果我问错了问题,我深表歉意。

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

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

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

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

更新 4:iOS 5 与 iOS 6?

所以我能注意到的是,这似乎是 iOS 5 与 iOS 6 的问题。在 iOS 6 上使用 line 测试时,GlassboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]下面的代码有效。但是,这条线GlassboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]在 iOS 5 中也不起作用。有什么想法吗?通过将标识符更改为protocell.

更新 3:我现在使用 GitHub!

这是相关的回购:https ://github.com/kenmhaggerty/Glassbox

更新 2:更多观察

所以我添加了,@synthesize tableView = _tableView因为我在某处读到了可能有帮助的响应,但我现在意识到它阻止了我的数据加载到我的表格视图中,即使在 iOS 模拟器上运行也是如此。注释掉那行代码会将代码返回到我上面描述的方式:它在 iOS 模拟器上工作得很好,但在dequeueReusableCellWithIdentifier:没有指定错误的情况下断线,只是Thread 1: breakpoint 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

让我知道我是否应该发布更多内容。

4

4 回答 4

2

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

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

于 2012-11-09T19:52:10.703 回答
0

请发布您的代码。我可以建议您保留 xib 文件并重uitableviewcell试。你使用一种细胞吗?或者你使用几种不同的风格?

===== 答案 =====

当您创建单元格GlassboxCell *cell并使用 dequeueReusableCellWIthIdentifier 时,您需要检查单元格是否已分配。

// use dequeueReusableCellWithIdentifier for init cell
if (cell == nil){
    cell = [[[GlassboxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:yourIdentifier]] autorelease];
}
//rest your code for init properties for cell
于 2012-10-25T00:45:07.207 回答
0

好的,尝试使用此功能而不是您的功能。

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

    if (cell == nil)
    {
        cell = [[GlassboxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: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];

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

    return cell;
}
于 2012-10-25T11:14:24.867 回答
0

我想我发现了问题......至少它适用于我的项目。

当您使用旧版本的 Xcode(例如 4)创建 UITableViewController 类时,生成的 cellForRowAtIndexPath 代码如下:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

但是,当您使用 Xcode 4.5.2(我的)制作 UITableViewController 时,生成的 cellForRowAtIndexPath 代码是这样的:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

所以在我的情况下,我所做的就是删除“forIndexPath:indexPath”部分,它可以工作!

(这是我的第一个答案,所以如果我搞砸了,请温柔)

于 2012-11-14T23:13:31.563 回答