2

gridViewのように機能するUITableViewの概念を実装しています。UITableViewにNSMutableArraysデータを読み込んでいます。tableViewCellのUIButtonに配列アイテムを読み込んでいます。ボタンを押すと、何らかのアクションを実行する必要があります。つまり、各行に4つのボタンがあります。行の数は配列アイテムの数に依存します。部分的に実行できます。ボタンをクリックすると、配列に8個または8個未満のアイテムがある限り、ボタンの次のアクションが実行されます。ただし、それを超える場合は次に、追加されたボタンを表示できますが、ボタンをクリックできませんでした(つまり、-(IBAction)buttonPressed:(id)sender {)応答しません..どこが間違っているのか理解できませんでした..?

sections=[[NSMutableArray alloc] init];
        for(int s=0;s<1;s++)
        {
            NSMutableArray *section=[[NSMutableArray alloc] init];
            for(int i=0;i<[arr1 count];i++)
            {
                Item *item=[[Item alloc] init];
                NSString *eventName=[[arr1 objectAtIndex:i]objectForKey:@"Time"];

                item.Time=eventName;
                [section addObject:item];

            }
            [sections addObject:section];

        }
        tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,430,320,200) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];   
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sectionItems=[sections objectAtIndex:indexPath.section];
    int numRows=[sectionItems count]/[arr1 count];
    return numRows * 80.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *hlCellID=@"hlCellID";

    UITableViewCell* hlcell=[tableView dequeueReusableCellWithIdentifier:hlCellID];

    if(hlcell == nil)
    {
        hlcell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:hlCellID]autorelease];

        hlcell.accessoryType = UITableViewCellAccessoryNone;
        hlcell.selectionStyle = UITableViewCellSelectionStyleNone;    
    }
    int section=indexPath.section;
    NSMutableArray *sectionItems=[sections objectAtIndex:section];
    int n=[sectionItems count];
    int i=0,i1=0;
    while(i<n)
    {
        int yy= 4+i1*34;
        int j=0;
        for(j=0;j<4;j++)
        {
            if(i>=n)break;
           Item *item=[sectionItems objectAtIndex:i];
            CGRect rect=CGRectMake(0+70*j,yy,79,40);
            UIButton *button=[[UIButton alloc] initWithFrame:rect];
            [button setFrame:rect];
            [button setContentMode:UIViewContentModeLeft];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            NSString *settitle=[NSString stringWithFormat:@"%@",item.Time];
            [button setTitle:settitle forState:UIControlStateNormal];
            NSString *tagValue=[NSString stringWithFormat:@"%d%d",indexPath.section+1,i];
            button.tag=[tagValue intValue];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setShowsTouchWhenHighlighted:YES];     
            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
           [hlcell.contentView addSubview:button];
            [button release];
            i++;




        }
        i1=i1+1;
    }
    return hlcell;

}



-(IBAction)buttonPressed:(id)sender
{

    int tagID=[sender tag];
    int divnum=0;
    if(tagID<100)
        divnum=10;
    else
        divnum=100;
    int section=[sender tag]/divnum;
    section-=1;
    int itemId=[sender tag]%divnum;


    NSMutableArray *sectionItems=[sections objectAtIndex:section];
    Item *item=[sectionItems objectAtIndex:itemId];

}
 In Item class I have NSString *Time;

私の配列は次のようなものです:

(
        {
        Time = "9:00 am";
    },
        {
        Time = "9:15 am";
    },
        {
        Time = "9:30 am";
    },
        {
        Time = "9:45 am";
    },
        {
        TimeStart = "10:00 am";
    },
        {
        Time = "10:15 am";
    },
......24 objects in the array

![すべてのボタンをクリックして-(IBAction)][1]に応答するにはどうすればよいですか。

4

4 に答える 4

1

これこれを試すことができます

もう 1 つの git サンプル: サンプル お役に立てば幸いです。

于 2012-11-21T13:04:21.040 に答える
0

UILabels を作成してテーブルビューに追加し、創造性を発揮してグリッド ビューのように表示することができます。

于 2013-01-14T10:10:16.637 に答える
0

こんにちは、あなたの答えが得られたことを願っています。そうでない場合は、次のコードを実装してください。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        int numRows=0;

        NSMutableArray *sectionItems = [sections objectAtIndex:indexPath.section];
        if ([sectionItems count]<5)
        {
              numRows = [sectionItems count]/1;
            numRows =numRows *152;

            NSLog(@"result less then 6 or zero");
        }
        else
        {
         numRows = [sectionItems count]/6;
            numRows =numRows *152.0;
        }
        //return numRows * 152.0;    //145
        return numRows;
    }

問題は、ボタンの高さを計算しているときです。numRows = [sectionItems カウント]/1; したがって、必要に応じてこの行を管理してください...

これがうまくいくことを願っています...

于 2013-04-16T13:28:34.800 に答える
0

iOS 6 をターゲットにしている場合は、常に UICollectionView があります。ドキュメントチュートリアル

于 2012-11-21T13:44:36.363 に答える