アプリ起動直後にUITableViewCellが表示されない。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
mokopoko
記事: 3
登録日時: 12年前

アプリ起動直後にUITableViewCellが表示されない。

#1

投稿記事 by mokopoko » 12年前

いつもお世話になっています。
XcodeでObjective-Cをやっております。
今、他の人の作品を参考に、テーブルをタッチして、新しいテーブルを表示するプログラムを作っています。
いま、最初に表示されるテーブルをA、タッチされて表示されるテーブルをBとします。
【問題】
Bに表示したいデータは表示されるのですが、初めから表示されているAには、何も表示されていません。
UIlabelだとうまくいったのですが、UITableViewCellを利用したいのです。が、うまくいきません。下にコードを記載させておきます(全部で、4つ)

ちなみに、URYView.mの330行目あたりにあるメソッド内に記述してあります。念のためそのコードも記載します。

コード:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{//Asks the delegate for a view object to display in the header of the specified section of the table view.
    UIView *view = [self invoke_viewForHeaderInSection:section];
    if (view)
    {
        CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
        CGRect frame = CGRectMake(0, 0, tableView.bounds.size.width, height);
        view.frame = frame;
        view.tag = section;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableViewHeaderTouchUpInside:)];
        [view addGestureRecognizer:tapGesture];
    }
    //ここから以下です!!!!!!!!!!!!!!!
    static NSString *cellIdentifier = @"URYFirstViewCell";
    _firstcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (_firstcell == nil)
    {
        _firstcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    _firstcell.textLabel.textColor = [UIColor blackColor];
    _firstcell.textLabel.text =@"これを表示させたい!!!!";
    _firstcell.backgroundView = view;
    _firstcell.selectionStyle = UITableViewCellSelectionStyleNone;
  //ここまでです!!!!!!!!!!!!!!!
    return view;
}
以上です。どうすればよいでしょうか?記述場所が間違えているのでしょうか?ガイダンス等によれば、次のメソッド内に記述すればいいとのことでしたが、そもそもこのメソッドはタッチしたあとでないと動きません。最初に表示されるテーブルには何も影響を及ぼしません。

コード:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   return [self invoke_cellForRowAtIndexPath:indexPath];
}

恐縮ですがよろしくお願いします。


以下URYView.hです。

コード:

#import <UIKit/UIKit.h>
#import "URYTableViewDelegate.h"
#import "URYTableDataSource.h"
@protocol URYTableViewDelegate,URYTableDataSource;
@interface URYView : UIView <UITableViewDelegate, UITableViewDataSource>{
    
}
@property (nonatomic,assign) id <URYTableDataSource> dataSource;
@property (nonatomic,assign) id <URYTableViewDelegate>   delegate;
@property (nonatomic,readonly,strong) NSIndexPath *openedIndexPath;
@property (nonatomic,readonly,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *atomView;//オレンジセル
@property (nonatomic) CGPoint atomOrigin;
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (void)sendCellTouchActionWithIndexPath:(NSIndexPath *)indexPath;
- (void)reloadData;

@end
以下URYView.mです。

コード:

#import "URYView.h"
typedef enum
{
	URYHeaderLineTouch,
	URYCellLineTouch,
	URYCodeSendTouch,
} URYLineTouchType;

static const CGFloat kDefultHeightForRow    = 44.0f;
static const CGFloat kDefultHeightForAtom   = 44.0f;
@interface URYView()
@property UITableViewCell* firstcell;
@end
@implementation URYView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //UITableViewCell* cell;
        _openedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];
        _atomOrigin = CGPointMake(0, 0);
        _tableView = [[UITableView alloc] initWithFrame:frame];
        _tableView.delegate         = self;
        _tableView.dataSource       = self;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.separatorStyle   = UITableViewCellSeparatorStyleNone;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [self addSubview:_tableView];
        //[_tableView reloadData];
    }
    return self;
}
#pragma mark - Public Methods

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
{
    NSLog(@"- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier");
    id cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
    return cell;
}

- (void)reloadData
{
    NSLog(@"- (void)reloadData");
    _openedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];
    [self.tableView reloadData];
}

#pragma mark - Private Methods

- (void)addTapGestureRecognizerForView:(UIView *)view action:(SEL)action
{
    if (view)
    {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewCellTouchUpInside:)];
        [view addGestureRecognizer:tapGesture];
    }
}


- (void)removeTapGestureRecognizerFromView:(UIView *)view
{
    if (view)
    {
        for (UIGestureRecognizer *gesture in view.gestureRecognizers)
        {
            if ([gesture.view isEqual:view])
            {
                [view removeGestureRecognizer:gesture];
            }
        }
    }
}
#pragma mark - Private Operation For Header Open & Close
- (NSMutableArray *)buildInsertRowWithSection:(NSInteger)section
{
    NSLog(@"- (NSMutableArray *)buildInsertRowWithSection:(NSInteger)section");
    int insert = [self invoke_numberOfRowsInSection:section];
    if (insert != 0)
    {
        [self invoke_willOpenHeaderAtSection:section];
    }
    _openedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:section];
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < insert; i++)
    {
        [indexPaths addObject: [NSIndexPath indexPathForRow:i inSection:section]];
    }
    return indexPaths;
}
- (NSMutableArray *)buildDeleteRowWithSection:(NSInteger)section
{
    int delete = [self invoke_numberOfRowsInSection:section];
    if (delete != 0)
    {
        [self invoke_willCloseHeaderAtSection:section];
    }
    
    if (section == self.openedIndexPath.section)
    {
        _openedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];
    }
    
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i = 0; i < delete; i++)
    {
        [indexPaths addObject: [NSIndexPath indexPathForRow:i inSection:section]];
    }
    return indexPaths;
}
- (void)openOrCloseHeaderWithSection:(NSInteger)section
{
    NSLog(@"- (void)openOrCloseHeaderWithSection:(NSInteger)section");
    NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
    NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
    
    NSInteger oldSection = self.openedIndexPath.section;
    NSInteger newSection = section;
    
    if (oldSection <= -1)
    {
        if (oldSection != newSection)
        {
            insertIndexPaths = [self buildInsertRowWithSection:newSection];
        }
    }
    else
    {
        if (oldSection != newSection && newSection > -1)
        {
            deleteIndexPaths = [self buildDeleteRowWithSection:oldSection];
            insertIndexPaths = [self buildInsertRowWithSection:newSection];
        }
        else
        {
            deleteIndexPaths = [self buildDeleteRowWithSection:oldSection];
        }
    }
    
    [self.tableView beginUpdates];
    if ([insertIndexPaths count] > 0)
    {
        [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
    if ([deleteIndexPaths count] > 0)
    {
        [self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}

#pragma mark - Private Operation For Row Open & Close
- (NSMutableArray *)buildWillOpenRowsWithRow:(NSInteger)row
{
    NSLog(@"- (NSMutableArray *)buildWillOpenRowsWithRow:(NSInteger)row");
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:self.openedIndexPath.section];
    
    [self invoke_willOpenRowAtIndexPath:indexPath];
    
    _openedIndexPath = indexPath;
    
    return [NSMutableArray arrayWithObject:indexPath];
}
- (NSMutableArray *)buildWillCloseRowsWithRow:(NSInteger)row
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:self.openedIndexPath.section];
    
    [self invoke_willCloseRowAtIndexPath:indexPath];
    
    if (row == self.openedIndexPath.row)
    {
        _openedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:self.openedIndexPath.section];;
    }
    
    return [NSMutableArray arrayWithObject:indexPath];
}
- (void)openOrCloseCellWithRow:(NSInteger)row
{
    NSLog(@"- (void)openOrCloseCellWithRow:(NSInteger)row");
    NSMutableArray *reloadIndexPaths = [NSMutableArray array];
    
    NSInteger oldRow = self.openedIndexPath.row;
    NSInteger newRow = row;
    
    if (oldRow <= -1)
    {
        if (oldRow != newRow)
        {
            reloadIndexPaths = [self buildWillOpenRowsWithRow:newRow];
            
        }
    }
    else
    {
        if (oldRow != newRow)
        {
            [reloadIndexPaths addObjectsFromArray: [self buildWillCloseRowsWithRow:oldRow]];
            [reloadIndexPaths addObjectsFromArray: [self buildWillOpenRowsWithRow:newRow]];
        }
        else
        {
            reloadIndexPaths = [self buildWillCloseRowsWithRow:oldRow];
        }
    }
    
    [self.tableView reloadRowsAtIndexPaths:reloadIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Private Operation For Row & Header
- (void)openOrCloseCellWithTouchType:(URYLineTouchType)touchType forIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"- (void)openOrCloseCellWithTouchType:(URYLineTouchType)touchType forIndexPath:(NSIndexPath *)indexPath");
    switch (touchType)
    {
        case URYHeaderLineTouch:
        {
            [self openOrCloseHeaderWithSection:indexPath.section];        }
            break;
        case URYCellLineTouch:
        {
            [self openOrCloseCellWithRow:indexPath.row];
        }
            break;
        case URYCodeSendTouch:
        {
            if (indexPath.section != self.openedIndexPath.section)
            {
                [self openOrCloseHeaderWithSection:indexPath.section];
                double delayInSeconds = 0.4;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    
                    [self openOrCloseCellWithRow:indexPath.row];
                });
            }
            else
            {
                [self openOrCloseCellWithRow:indexPath.row];
            }
        }
            break;
        default:
            
            break;
    }
}

#pragma mark - Touch Selector
- (void)tableViewHeaderTouchUpInside:(UITapGestureRecognizer *)gesture
{
    int section = gesture.view.tag;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:-1 inSection:section];
    
    [self openOrCloseCellWithTouchType:URYHeaderLineTouch forIndexPath:indexPath];
}
- (void)tableViewCellTouchUpInside:(UITapGestureRecognizer *)gesture
{
    UITableViewCell *cell = (UITableViewCell *)gesture.view;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    CGFloat h = [self invoke_heightForRowAtIndexPath:indexPath];
    CGFloat w = cell.bounds.size.width;
    CGRect rect = CGRectMake(0, 0, w, h);
    CGPoint point = [gesture locationInView:cell];
    if(CGRectContainsPoint(rect,point))
    {
        
        [self openOrCloseCellWithTouchType:URYCellLineTouch forIndexPath:indexPath];
    }
    
}

#pragma mark - Table View Delegate
//Tells the delegate the table view is about to draw a cell for a particular row.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self addTapGestureRecognizerForView:cell action:@selector(tableViewCellTouchUpInside:)];
    
    if ([self.openedIndexPath compare:indexPath] == NSOrderedSame)
    {
        CGFloat atomViewHeight = [self invoke_heightForAtomAtIndexPath:indexPath];
        CGFloat cellViewHeight = [self invoke_heightForRowAtIndexPath:indexPath];
        //cell.textLabel.text = @"hohoho";
        self.atomView.frame = CGRectMake(self.atomOrigin.x,self.atomOrigin.y + cellViewHeight,cell.bounds.size.width, atomViewHeight);
        [cell addSubview:self.atomView];
        //[cell bringSubviewToFront:self.atomView];
    }
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    [self removeTapGestureRecognizerFromView:cell];
    
    if ([cell.subviews containsObject:self.atomView])
    {
        [self.atomView removeFromSuperview];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//Asks the delegate for the height to use for a row in a specified location.
    CGFloat h = [self invoke_heightForRowAtIndexPath:indexPath];
    
    if ([self.openedIndexPath compare:indexPath] == NSOrderedSame)
    {
        h += [self invoke_heightForAtomAtIndexPath:indexPath];
    }
    
    return h;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{//Asks the delegate for a view object to display in the header of the specified section of the table view.
    UIView *view = [self invoke_viewForHeaderInSection:section];
    if (view)
    {
        CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
        CGRect frame = CGRectMake(0, 0, tableView.bounds.size.width, height);
        view.frame = frame;
        view.tag = section;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableViewHeaderTouchUpInside:)];
        [view addGestureRecognizer:tapGesture];
    }
    
    static NSString *cellIdentifier = @"URYFirstViewCell";
    _firstcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (_firstcell == nil)
    {
        NSLog(@"rerarererer");
        _firstcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    _firstcell.textLabel.textColor = [UIColor blackColor];
    _firstcell.textLabel.text =@"この文字を入力させたい!!!!";
    _firstcell.backgroundView = view;
    _firstcell.selectionStyle = UITableViewCellSelectionStyleNone;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{//Asks the delegate for the height to use for the header of a particular section. This method allows the delegate to specify section headers with varying height
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)])
    {
        return [self invoke_heightForHeaderInSection:section];
    }
    else
    {
        return 0;
    }
}

#pragma mark - Table View DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)] && self.openedIndexPath.section != section)
    {
        return 0;
    }
    
    NSInteger n = 0;
    n = [self invoke_numberOfRowsInSection:section];
    return n;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//
    return [self invoke_cellForRowAtIndexPath:indexPath];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  [self invoke_numberOfSectionsInTableView];
}

#pragma mark - Invoke Delegate

- (CGFloat)invoke_heightForHeaderInSection:(NSInteger)section
{
    NSInteger h = 0;
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: heightForHeaderInSection:)])
    {
        h = [self.delegate mTableView:self heightForHeaderInSection:section];
    }
    return h;
}

- (CGFloat)invoke_heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = kDefultHeightForRow;
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: heightForRowAtIndexPath:)])
    {
        h = [self.delegate mTableView:self heightForRowAtIndexPath:indexPath];
    }
    return h;
}

- (CGFloat)invoke_heightForAtomAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = kDefultHeightForAtom;
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: heightForAtomAtIndexPath:)])
    {
        h = [self.delegate mTableView:self heightForAtomAtIndexPath:indexPath];
    }
    return h;
}

- (UIView *)invoke_viewForHeaderInSection:(NSInteger)section
{
    UIView *view = nil;
    if(self.delegate && [self.delegate respondsToSelector:@selector(mTableView: viewForHeaderInSection:)])
    {
        view = [self.delegate mTableView:self viewForHeaderInSection:section];
    }
    return view;
}

- (void)invoke_willOpenHeaderAtSection:(NSInteger)section
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: willOpenHeaderAtSection:)])
    {
        [self.delegate mTableView:self willOpenHeaderAtSection:section];
    }
}

- (void)invoke_willCloseHeaderAtSection:(NSInteger)section
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: willOpenRowAtIndexPath:)])
    {
        [self.delegate mTableView:self willCloseHeaderAtSection:section];
    }
}

- (void)invoke_willOpenRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: willOpenHeaderAtSection:)])
    {
        [self.delegate mTableView:self willOpenRowAtIndexPath:indexPath];
    }
}

- (void)invoke_willCloseRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(mTableView: willCloseRowAtIndexPath:)])
    {
        [self.delegate mTableView:self willCloseRowAtIndexPath:indexPath];
    }
}


#pragma mark - Invoke DataSource

- (NSInteger)invoke_numberOfRowsInSection:(NSInteger)section
{
    NSInteger n = 0;
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(mTableView: numberOfRowsInSection:)])
    {
        n = [self.dataSource mTableView:self numberOfRowsInSection:section];
    }
    return n;
}

- (UITableViewCell *)invoke_cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(mTableView: cellForRowAtIndexPath:)])
    {
        cell = [self.dataSource mTableView:self cellForRowAtIndexPath:indexPath];
    }
    return cell;
}
/*
- (UITableViewCell *)invoke_cellForHeader:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(mTableView: cellForHeader::)])
    {
        cell = [self.dataSource mTableView:self cellForRowAtIndexPath:indexPath];
    }
    return cell;
}
*/
- (NSInteger)invoke_numberOfSectionsInTableView
{
    NSInteger n = 1;
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)])
    {
        n = [self.dataSource numberOfSectionsInTableView:self];
    }
    return n;
}
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
@end
以下URYViewController.hです。

コード:

#import <UIKit/UIKit.h>
#import "URYTableViewDelegate.h"
#import "URYTableDataSource.h"
@protocol URYTableViewDelegate,URYTableDataSource;
@interface URYView : UIView <UITableViewDelegate, UITableViewDataSource>{
    
}
@property (nonatomic,assign) id <URYTableDataSource> dataSource;
@property (nonatomic,assign) id <URYTableViewDelegate>   delegate;
@property (nonatomic,readonly,strong) NSIndexPath *openedIndexPath;
@property (nonatomic,readonly,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *atomView;
@property (nonatomic) CGPoint atomOrigin;
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (void)sendCellTouchActionWithIndexPath:(NSIndexPath *)indexPath;
//- (void)sendHeaderTouchActionWithSection:(NSInteger)section;
- (void)reloadData;

@end

以下URYViewController.mです。

コード:

#import "URYViewController.h"
#import "URYView.h"
@interface URYViewController ()

@end
@implementation URYViewController
//@synthesize mTableView = _mTableView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect rect = [UIScreen mainScreen ].bounds;
    
    
    self.mTableView = [[URYView alloc] initWithFrame:rect];
    self.mTableView.dataSource = self;
    self.mTableView.delegate   = self;
    self.mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.mTableView];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.mTableView.bounds.size.width, 100)];
    view.backgroundColor = [UIColor colorWithRed:234/255.0 green:125/255.0 blue:91/255.0 alpha:1];
    self.mTableView.atomView = view;
    URYTextEdit* inputText = [[URYTextEdit alloc] init];
    [inputText ReadPlist:@"horizongrade2words223"];
    [inputText determineQuestionRangeofUnit:@"Unit1"];
	// Do any additional setup after loading the view, typically from a nib.
}

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

#pragma mark - URYTableViewDataSource

- (NSInteger)mTableView:(URYView *)mTableView numberOfRowsInSection:(NSInteger)section
{  return 4;
}

- (UITableViewCell *)mTableView:(URYView *)mTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"URYViewCell";
    NSLog(@"UryViewController内のuitableviewcell");
    UITableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    UIView *view = [[UIView alloc] initWithFrame:cell.bounds] ;
    view.layer.backgroundColor  = [UIColor colorWithRed:246/255.0 green:213/255.0 blue:105/255.0 alpha:1].CGColor;
    view.layer.masksToBounds    = YES;
    view.layer.borderWidth      = 0.5;
    view.layer.borderColor      = [UIColor colorWithRed:250/255.0 green:77/255.0 blue:83/255.0 alpha:1].CGColor;
    cell.backgroundView = view;
    cell.textLabel.text =@"これは表示されている。";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSLog(@"cellcellcell%@",cell);
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(URYView *)mTableView
{
    return 8;
}
#pragma mark - Table view delegate

- (CGFloat)mTableView:(URYView *)mTableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (CGFloat)mTableView:(URYView *)mTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 66;
}

- (CGFloat)mTableView:(URYView *)mTableView heightForAtomAtIndexPath:(NSIndexPath *)indexPath
{
    return 10;
}

- (UIView *)mTableView:(URYView *)mTableView viewForHeaderInSection:(NSInteger)section
{  
    UIView *header = [[UIView alloc] initWithFrame:mTableView.bounds];
    header.layer.backgroundColor    = [UIColor colorWithRed:218/255.0 green:249/255.0 blue:255/255.0 alpha:1].CGColor;
    header.layer.masksToBounds      = NO;
    header.layer.borderWidth        = 0.5;
    header.layer.borderColor        = [UIColor colorWithRed:179/255.0 green:143/255.0 blue:195/255.0 alpha:1].CGColor;
    return header;
}

#pragma mark - Header Open Or Close

- (void)mTableView:(URYView *)mTableView willOpenHeaderAtSection:(NSInteger)section
{
    NSLog(@"Open Header ----%d",section);//ブルー
}

- (void)mTableView:(URYView *)mTableView willCloseHeaderAtSection:(NSInteger)section
{
    NSLog(@"Close Header ---%d",section);
}

#pragma mark - Row Open Or Close

- (void)mTableView:(URYView *)mTableView willOpenRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Open Row ----%d",indexPath.row);//イエロー
}

- (void)mTableView:(URYView *)mTableView willCloseRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Close Row ----%d",indexPath.row);
}

@end


mokopoko
記事: 3
登録日時: 12年前

Re: アプリ起動直後にUITableViewCellが表示されない。

#2

投稿記事 by mokopoko » 12年前

申し訳ございません。プロトコルファイルの記載を忘れておりました!2つございます。

URYTableViewDelegate.hです。

コード:

#import <Foundation/Foundation.h>
@class URYView;
@protocol URYTableViewDelegate <NSObject>
@optional

- (CGFloat)mTableView:(URYView *)mTableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)mTableView:(URYView *)mTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)mTableView:(URYView *)mTableView heightForAtomAtIndexPath:(NSIndexPath *)indexPath;

- (UIView *)mTableView:(URYView *)mTableView viewForHeaderInSection:(NSInteger)section;

- (void)mTableView:(URYView *)mTableView willOpenHeaderAtSection:(NSInteger)section;
- (void)mTableView:(URYView *)mTableView willCloseHeaderAtSection:(NSInteger)section;

- (void)mTableView:(URYView *)mTableView willOpenRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)mTableView:(URYView *)mTableView willCloseRowAtIndexPath:(NSIndexPath *)indexPath;


URYTableDataSource.h です。

コード:

#import <Foundation/Foundation.h>
@class URYView;
@protocol URYTableDataSource <NSObject>
@required

- (NSInteger)mTableView:(URYView *)mTableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)mTableView:(URYView *)mTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
//- (UITableViewCell *)mTableView:(URYView *)mTableView cellForHeader:(NSIndexPath *)indexPath;
@optional

- (NSInteger)numberOfSectionsInTableView:(URYView *)mTableView;              // Default is 1 if not implemented

よろしくお願いいたします。

mokopoko
記事: 3
登録日時: 12年前

Re: アプリ起動直後にUITableViewCellが表示されない。

#3

投稿記事 by mokopoko » 12年前

解決しました。下記のように[header addSubview:_firstcell];を入れて、URYViewControllerの
- (UIView *)mTableView:(URYView *)mTableView viewForHeaderInSection:(NSInteger)sectionにUITableViewCellメソッドを入れてあげると表示されました。
つまらないミスでした。次回もまたよろしくお願いします。

コード:


- (UIView *)mTableView:(UITableView *)mTableView viewForHeaderInSection:(NSInteger)section
{//Asks the delegate for a view object to display in the header of the specified section of the table view.
    UIView *view = [self invoke_viewForHeaderInSection:section];
    if (view)
    {
        CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
        CGRect frame = CGRectMake(0, 0, tableView.bounds.size.width, height);
        view.frame = frame;
        view.tag = section;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableViewHeaderTouchUpInside:)];
        [view addGestureRecognizer:tapGesture];
    }
    //ここから以下です!!!!!!!!!!!!!!!
    static NSString *cellIdentifier = @"URYFirstViewCell";
    _firstcell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (_firstcell == nil)
    {
        _firstcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    _firstcell.textLabel.textColor = [UIColor blackColor];
    _firstcell.textLabel.text =@"これを表示させたい!!!!";
    _firstcell.backgroundView = view;
    _firstcell.selectionStyle = UITableViewCellSelectionStyleNone;
  //ここまでです!!!!!!!!!!!!!!!
   [header addSubview:_firstcell];
    return view;
}

閉鎖

“C言語何でも質問掲示板” へ戻る