【问题标题】:TableView with 2 colums by row , iphone/ipad逐行 2 列的表格视图,iphone/ipad
【发布时间】:2011-05-14 11:16:48
【问题描述】:

谁能告诉我如何创建一个tableview 在 Iphone SDK 中按列有两个单元格??

这样下单:

  _____________
  |  1  |   2  |
  |_____|______|
  |  3  |   4  |
  |_____|______|

最好的问候

【问题讨论】:

  • 你能给我们举个例子,你想在单元格中放什么吗?
  • 每个单元格将有一张带有两个标签的图片。这将是新“新闻”的介绍,用户按下单元格后,它将打开一个包含所有“新闻”的新视图。

标签: iphone ios ios4 uitableview


【解决方案1】:

在 ios UITableView 中,您只能拥有一列/单元格。如果你想要更多的属性,在同一个单元格中,你需要通过子类化 UITableViewCell 来创建一个自定义表格视图单元格。在界面生成器中,您可以自定义布局。教程:http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/

你的问题的具体例子:

.h 文件:

 #import <UIKit/UIKit.h>


@interface MyCell : UITableViewCell {
    UIButton * column1;
    UIButton * column2;

    UILabel* column1_label1;
    UILabel* column1_label2;

    UILabel* column2_label1;
    UILabel* column2_label2;
}

@property (nonatomic,retain) UIButton * column1;
@property (nonatomic,retain) UIButton * column2;

@property (nonatomic,retain) UILabel* column1_label1;
@property (nonatomic,retain) UILabel* column1_label2;

@property (nonatomic,retain) UILabel* column2_label1;
@property (nonatomic,retain) UILabel* column2_label2;

@end

.m 文件:

#import "MyCell.h"


@implementation MyCell

@synthesize column1,column1_label1,column1_label2,column2,column2_label1,column2_label2;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        column1 = [UIButton buttonWithType: UIButtonTypeCustom];
        column1.tag = 1;
        column1.autoresizingMask = 292;

        column1.backgroundColor = [UIColor redColor];
        column1.frame = CGRectMake(0, 0, self.contentView.bounds.size.width/2,  self.contentView.bounds.size.height);


        column1_label1 = [[UILabel alloc] initWithFrame: CGRectMake(column1.bounds.size.width*1/5,
                                                                             0,
                                                                             column1.bounds.size.width*4/5,
                                                                             column1.bounds.size.height/2)];

        column1_label1.backgroundColor = [UIColor redColor];
        [column1 addSubview:column1_label1]; 

        column1_label2 = [[UILabel alloc] initWithFrame: CGRectMake(column1.bounds.size.width*1/5,
                                                                             column1.bounds.size.height/2,
                                                                             column1.bounds.size.width*4/5,
                                                                             column1.bounds.size.height/2)];

        column1_label2.backgroundColor = [UIColor greenColor];
        [column1 addSubview:column1_label2]; 

        [self.contentView addSubview: column1]; 




        column2 = [UIButton buttonWithType: UIButtonTypeCustom];


        column2.tag = 2;
        column2.autoresizingMask = 292;

        column2.backgroundColor = [UIColor greenColor];
        column2.frame = CGRectMake(self.contentView.bounds.size.width/2, 0, self.contentView.bounds.size.width/2,  self.contentView.bounds.size.height);

        column2_label1 = [[UILabel alloc] initWithFrame: CGRectMake(column2.bounds.size.width*1/5,
                                                                             0,
                                                                             column2.bounds.size.width*4/5,
                                                                             column2.bounds.size.height/2)];

        column2_label1.backgroundColor = [UIColor redColor];
        [column2 addSubview:column2_label1]; 

        column2_label2 = [[UILabel alloc] initWithFrame: CGRectMake(column2.bounds.size.width*1/5,
                                                                             column2.bounds.size.height/2,
                                                                             column2.bounds.size.width*4/5,
                                                                             column2.bounds.size.height/2)];

        column2_label2.backgroundColor = [UIColor greenColor];
        [column2 addSubview:column2_label2]; 


        [self.contentView addSubview: column2];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc
{
    [super dealloc];
}

@end

UITableViewController 中的用法:

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 40;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    MyCell * mycell2 = ((MyCell*)cell);
    if (cell == nil) {
        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        [mycell2.column1 addTarget:self action:@selector(column1Selected:) forControlEvents:UIControlEventTouchUpInside];
        [mycell2.column2 addTarget:self action:@selector(column1Selected:) forControlEvents:UIControlEventTouchUpInside];        
    }

    // Configure the cell...

    mycell2.column1_label1.text = @"column1_label1";
    mycell2.column1_label2.text = @"column1_label2";
    mycell2.column1.tag = indexPath.row*2;
    mycell2.column2_label1.text = @"column2_label1";
    mycell2.column2_label2.text = @"column2_label2";
    mycell2.column2.tag = indexPath.row*2 +1;
    return cell;
}

- (void) column1Selected: (id) sender
{

    UIAlertView *alert = [[ UIAlertView alloc]                          
                          initWithTitle: @" Alert"                          
                          message: [NSString stringWithFormat: @"button %d",((UIButton *) sender).tag]
                          delegate: nil                          
                          cancelButtonTitle: @" OK"                          
                          otherButtonTitles: nil];    
    [alert show] ;
    [alert release];
}

【讨论】:

  • 我的问题是,例如,当用户触摸单元格“1”时,它会打开 view_1,当单元格 2 时,打开 view_2。有什么方法可以做到这一点,只需在界面中逐行使用 2 个假定的列???
  • 单元格可以是按钮,而不是标签,您可以为按钮添加操作处理程序,如下所示: [column1 addTarget:self action:@selector("whenColumn1Selected:") forControlEvents:(UIControlEvents)UIControlEventTouchUpInside]; where whenColumn1Selected: is a -(void) whenColumn1Selected { //在选择时做某事}
  • 如果您在设计中不需要它,您可以关闭原始单元格选择。
  • 我将它编辑到单元格 1 和单元格 2 的行为就像一个按钮,用导航控制器代码和宾果游戏替换警报!
  • 拳头,非常感谢您的回答....问题是每列都有一个带有两个标签的图像,这需要一个视图,对吗??我可以为视图添加一个动作处理程序吗???
【解决方案2】:

在 iOS6 中你可以使用UICollectionView

如果您需要 iOS5 支持,AQGridView 是一个网格控件,可以满足您的需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多