Objective c 为什么自定义UITableViewCells可以在iOS 6中工作而不能在iOS 5中工作?
我在尝试在iPhone上使用表视图时遇到了很多麻烦。奇怪的是,它似乎在我的iOS模拟器上完全正常工作(即,我可以向数组添加一个条目,该条目显示在我的表视图中)。但是,当我在使用iOS设备时尝试添加条目时,代码在Objective c 为什么自定义UITableViewCells可以在iOS 6中工作而不能在iOS 5中工作?,objective-c,uitableview,subclass,reloaddata,Objective C,Uitableview,Subclass,Reloaddata,我在尝试在iPhone上使用表视图时遇到了很多麻烦。奇怪的是,它似乎在我的iOS模拟器上完全正常工作(即,我可以向数组添加一个条目,该条目显示在我的表视图中)。但是,当我在使用iOS设备时尝试添加条目时,代码在dequeueReusableCellWithIdentifier:行中断。我已检查大小写不一致、名称不一致,已在自定义UITableViewCell子类中重新实现了prepareforuse,并尝试使用IBV在我的UITableViewCell子类中定义字段。标签,也许还有其他一些东西,
dequeueReusableCellWithIdentifier:
行中断。我已检查大小写不一致、名称不一致,已在自定义UITableViewCell子类中重新实现了prepareforuse
,并尝试使用IBV在我的UITableViewCell子类中定义字段。标签,也许还有其他一些东西,但没有一个起作用
这个问题与我的上一个问题无关:
编程的难点在于总是知道该问哪个问题,所以如果我问错了问题,我道歉
更新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和iOS 6如何处理自定义UITableViewCell子类之间的问题。尚未找到解决方案:(
更新4:iOS 5与iOS 6?
因此,我所能注意到的是,这似乎是iOS 5与iOS 6的一个问题。当在iOS 6上使用lineGlassboxCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
进行测试时,下面的代码不起作用。但是,这一行和GlassboxCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
在iOS 5中工作。有什么想法吗?我通过将标识符更改为protocell
,使它只工作了一次
更新3:我现在使用GitHub!
以下是相关回购协议:
更新2:更多观察结果
因此,我添加了@synthetic tableView=\u 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.h
//玻璃盒
//
//由Ken M.Haggerty于2012年10月22日创作。
//版权所有(c)2012 Ken M.Haggerty。保留所有权利。
//
#pragma标记-//注释(公共)//
#pragma标记-//导入(公共)//
#进口
#pragma标记-//协议//
//@协议GlassboxTableViewDatasource
//@属性(非原子,弱)NSMutableArray*arrayOfPlayers;
//@结束
#pragma标记-//定义(公共)//
@界面GlassboxTableViewController:UITableViewController
@属性(非原子,强)NSMUTABLEARRY*arrayOfPlayers;
-(iAction)addPlayer:(UIBarButtonItem*)发送方;
//@属性(非原子,强)id数据源;
@结束
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
//
//GlassboxTableViewController.m
//玻璃盒
//
//由Ken M.Haggerty于2012年10月22日创作。
//版权所有(c)2012 Ken M.Haggerty。保留所有权利。
//
#pragma标记-//注释(专用)//
#pragma标记-//导入(专用)//
#导入“GlassboxTableViewController.h”
#导入“GlassboxCell.h”
#导入“Player.h”
#进口
#pragma标记-//定义(专用)//
#定义侧边栏宽度百分比0.75
@界面GlassboxTableViewController()
//@属性(非原子,弱)IBUITableView*tableView;
-(无效)设置;
@结束
@GlassboxTableViewController的实现
#pragma标记-//setter和getter//
@合成arrayOfPlayers=_arrayOfPlayers;
@综合tableView=\u tableView;
//@综合数据源=_数据源;
-(void)SetArrayOffLayers:(NSMutableArray*)ArrayOffLayers
{
_ArrayOffLayers=ArrayOffLayers;
}
-(NSMutableArray*)arrayOfPlayers
{
如果(!\u ArrayOfLayers)\u ArrayOfLayers=[[NSMutableArray alloc]init];
//[_arrayofplayersaddobject:[[Player alloc]initWithUsername:@“Ken H.:”];
返回_arrayOfPlayers;
}
#pragma标记-//初始化和加载//
-(无效)设置
{
self.tableView.dataSource=self;
self.tableView.delegate=self;
}
-(id)initWithStyle:(UITableViewStyle)样式
{
NSLog(@“[initWithStyle]”);
self=[super initWithStyle:style];
如果(自我){
[自我设置];
}
回归自我;
}
-(无效)viewDidLoad
{
NSLog(@“[viewDidLoad]”);
[超级视图下载];
[自我设置];
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
//-(无效)视图显示:(BOOL)动画
//{
//[超级视图显示:动画];
//[self.view setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.width*边栏_width_PERCENT,self.view.frame.size.height)];
//}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma mark-//公共函数//
-(iAction)addPlayer:(用户界面
// use dequeueReusableCellWithIdentifier for init cell
if (cell == nil){
cell = [[[GlassboxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:yourIdentifier]] autorelease];
}
//rest your code for init properties for cell
- (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;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];