【问题标题】:2 buttons side-by-side in a UITableViewCellUITableViewCell 中并排的 2 个按钮
【发布时间】:2011-12-25 19:28:03
【问题描述】:

在 twitter iPhone 应用程序中,在“我的个人资料”视图中,有一个包含两行的部分,其中显示了关注、关注者、推文和收藏夹的统计信息,每一个似乎都具有 UIButton 的效果。现在我想知道它是如何创建的 - 为每个按钮使用图像?更清楚地说,它类似于以下内容。

|-----------------------|
|   23       |    11    |
| Following  |  tweets  | <-- tableview cell 1
|            |          |
|-----------------------|
|    3       |    11    |
| Followers  |favorites | <--- tableview cell 2
|            |          |
|-----------------------|

【问题讨论】:

    标签: iphone twitter uitableview uibutton


    【解决方案1】:

    不知道您的 Twitter 应用程序,但您可以创建一个自定义单元格并向其添加两个按钮。然后在实例化单元格时,您可以根据需要指定选择器和自定义背景图像。

    @interface CustomCell : UITableViewCell {
    UIButton* leftButton;
    UIButton* rightButton;
    }
    @property(nonatomic, retain) IBOutlet UIButton* leftButton;
    @property(nonatomic, retain) IBOutlet UIButton* rightButton;
    @end
    
    @implementation CustomCell
    
    @synthesize leftButton, rightButton;
    
    -(id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString*)reuseIdentifier {
        if( self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier] ) {
            // init
        }
        return self;
    }
    
    -(void) setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // configure for selected
    }
    
    - (void)dealloc {
        [rightButton release];
        [leftButton release];
        [super dealloc];
    }
    
    @end
    
    // in root view controller:
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"CustomCellIdentifier";
    
        // Dequeue or create a cell of the appropriate type.
        CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
            for( id oneObject in nib ) {
                if( [oneObject isKindOfClass:[CustomCell class]] ) cell = (CustomCell*)oneObject;
            }
        }
    
        NSInteger row = indexPath.row;
    
        UIButton* tempButton = cell.leftButton;
        tempButton.tag = row * 2;
        [tempButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setBackgroundImage:[thumbnails objectAtIndex:(row * 2)] forState:UIControlStateNormal];
    
        tempButton = cell.rightButton;
        tempButton.tag = (row * 2) + 1;
        [tempButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setBackgroundImage:[thumbnails objectAtIndex:((row * 2) + 1)] forState:UIControlStateNormal];
    
        return cell;
    }
    

    这段代码有点旧,但它说明了这一点。

    【讨论】:

    • 谢谢!所以我确实需要创建自定义按钮。
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多