これには多くのコードが含まれることになります。申し訳ありませんが、非常に問題なのは、このプログラムをステップ実行して、UITableViewのセクションを作成している場所を確認するのが難しいことです。
これは「iOSプログラミング:ビッグナードランチガイド」のテーブルビュー演習として始まり、これをもっと探求したいと思ったので、イラストレーターでいくつかの画像を作成して町に行きました。
私は正常に作成しました:
UIView
本の例のように、ランダムに生成された文字列である「アイテム」で埋められた、1つの単一セクションのUITableViewとUITableViewCell。(ランダムな形容詞、ランダムな名詞、ランダムなシリアル番号など)
タップするとハイライト状態に変わるその他のボタン
正常に機能しているように見えるボタンをさらにいくつか表示する最初の唯一のセクションの後のフッター
私が今やろうとしているのは、演習として、これらのランダムに生成された文字列アイテムの半分を取り、テーブルビューのセルに貼り付けて、テーブルビューのセクション2に配置することだけです。比較的簡単な作業のようですが、これをどこで/どのように行うかがわかりません。ドキュメントを読んだので、insertSections:withRowAnimation:メソッドを実装する必要があると思いますが、オンラインで、自分にとって意味のある方法で実装する方法の手がかりとなる例を見つけることができません。プログラム。
ショートカットするために-うまくいけば-問題はこの投稿の下部にあるItemsViewController.mファイルにあると思います。ItemStoreクラスは、前に説明したランダムに生成された文字列アイテムを保持する「allItems」と呼ばれる配列です。私がやろうとしていることにとって重要なことは、AppDelegateファイルでも起こっていないようです。どんな助けでもいただければ幸いです。よろしくお願いします。
ItemStore.h
#import <Foundation/Foundation.h>
@class Item;
@interface ItemStore : NSObject {
NSMutableArray *allItems;
}
+(ItemStore *)sharedStore;
-(NSMutableArray *)allItems;
-(Item *)createItem;
@end
ItemStore.m
#import "ItemStore.h"
#import "Item.h"
@implementation ItemStore
// create the singleton ItemStore
+(ItemStore *)sharedStore {
static ItemStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil] init];
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone {
return [self sharedStore];
}
-(id)init {
self = [super init];
if (self) {
allItems = [[NSMutableArray alloc] init];
}
return self;
}
-(NSArray *)allItems {
return allItems;
}
-(Item *)createItem {
Item *p = [Item randomItem];
[allItems addObject:p];
return p;
}
@end
AppDelegate.h
#import <UIKit/UIKit.h>
@class ItemsViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ItemsViewController *viewController;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ItemsViewController.h"
#import "Item.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ItemsViewController *itemsViewController = [[ItemsViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController];
[[self window] setRootViewController:navController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
@end
ItemsViewController.h
#import <Foundation/Foundation.h>
@interface ItemsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UIView *underView;
UITableView *ivarTableView;
}
@property (nonatomic, readwrite) UITableView *ivarTableView;
-(id)init;
-(id)initWithStyle:(UITableViewStyle)style;
-(void)insertSections:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
@end
ItemsViewController.m
#import "ItemsViewController.h"
#import "Item.h"
#import "ItemStore.h"
@implementation ItemsViewController
@synthesize ivarTableView;
-(void)viewDidLoad {
[super viewDidLoad];
CGFloat startingPoint = 33.0;
CGRect bounds = self.view.bounds;
bounds.origin.y = startingPoint;
bounds.size.height -= startingPoint;
self.ivarTableView = [[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped];
self.ivarTableView.dataSource = self;
self.ivarTableView.delegate = self;
self.ivarTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.ivarTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.ivarTableView];
// create background, custom navigation bar, and a few other images/buttons,
// and some buttons that get placed in the footer. all of these work fine.
// ...
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
// this method is a little unclear to me, but I've got everything in the footer working fine, so I haven't worried about it at this point.
return 0.0;
}
// so far, init{} just creates a random amount of 'Items', which are just random strings so there's something to populate the UITableViewCells.
-(id) init {
if (self) {
int r = arc4random() % 10;
NSLog(@"Number of cells in the table view should be: %d", r);
for (int i = 1; i <= r; i++) {
[[ItemStore sharedStore] createItem];
}
}
return self;
}
-(void)insertSections:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation{
// I think this is where I need to--create another section?--and send the second section the data that is going to be used in the second section's cells, but I'm having trouble tracing the flow of this program.
}
// Asks the delegate for the height to use for a row in a specified location and sends
// the appropriate size, based upon which image is going to be displayed for the particular cell.
// Top cell is a taller height, middle and bottom cell(s) are the same height.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger lastRow = [self tableView:ivarTableView numberOfRowsInSection:1] - 1;
NSInteger rowsAmount = [self tableView:ivarTableView numberOfRowsInSection:[indexPath section]];
if (indexPath.row == 0 && rowsAmount != 1) {
return 95.0;
} else if (indexPath.row == 0 && rowsAmount == 1) {
return 137.0;
} /*end change*/ else if (indexPath.row == lastRow) {
return 74.0;
} else {
return 74.0;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"];
}
NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [self tableView:ivarTableView numberOfRowsInSection:[indexPath section]];
if (rowsAmount == 1) {
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellTypeOne.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellTypeOneTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
} else if (rowsAmount == 2) {
if ([indexPath section] == 0 && [indexPath row] == 0) {
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellTypeOneMulti.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellTypeOneMultiTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
} else {
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBottom.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBottomTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
}
} else if (rowsAmount >= 3) {
if ([indexPath section] == 0 && [indexPath row] == 0){
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellTypeOneMulti.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellTypeOneMultiTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
} else if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBottom.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBottomTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
} else {
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellMiddle.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellMiddleTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
}
}
// gets rid of the white box that would bound the text of the cell
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
/* Set the text of the cell to the description of the item that is at the nth index of items, where n = row this cell will appear in on the tableView */
Item *p = [[[ItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
[[cell textLabel] setHighlightedTextColor:[UIColor whiteColor]];
[[cell textLabel] setTextColor:[UIColor blackColor]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
/* return count of ItemStore's allItems array */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[ItemStore sharedStore] allItems] count];
}
// ...
@end