基于自定义单元格创建UITableView

发布于 2024-12-20 17:53:30 字数 282 浏览 3 评论 0原文

我有一个问题。 我希望我的表格显示我的自定义单元格。每个单元格都包含一个按钮(请参阅附件)

在此处输入图像描述

在 cellForRowAtIndexPath 方法中,我返回自定义单元格。自定义单元格包含一个 UIButton,如附图所示。

我的问题如下 - 当我按下按钮时,它应该亮起(变为红色),其他按钮应该减弱(变为蓝色)。如何建立对象之间的联系?

任何答案我都会很高兴!谢谢!

I have a problem.
I want my table displays my custom cell. Each cell contains a button (see the attached file)

enter image description here

In the method cellForRowAtIndexPath I return the custom cell. The custom cell contains a UIButton as shown in attached image.

My problem is as follow - when i pressed the button it should light up (to red color) and other button should be attenuate (to blue color). How to make a connection between objects?

I would be happy with any answers! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2024-12-27 17:53:30

我解决了这个问题如下:

这是我的 UITableViewController 类的实现。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.delegate = self;
    [cells addObject:cell];

    // Configure the cell...

    return cell;
}

- (void)mustDeselectWithout:(CustomCell *)cell{
    for (CustomCell * currentCell in cells) {
        if(currentCell != cell){
            [currentCell deselect];
        }
    }
}

正如您所看到的,当我创建单元格时,我在 cellForRowAtIndexPath 方法中返回自定义单元格。

这是自定义单元格

.h

#import <UIKit/UIKit.h>
#import "ProtocolCell.h"

@interface CustomCell : UITableViewCell {
    UIButton * button;
}
@property(nonatomic, assign) id<ProtocolCell> delegate;
- (void)select;
- (void)deselect;
@end

和 .m

#import "CustomCell.h"


@implementation CustomCell
@synthesize delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(5, 5, 25, 25)];
        [button setBackgroundColor:[UIColor blueColor]];
        [button addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
    }
    return self;
}

- (void)select{
    [button setBackgroundColor:[UIColor redColor]];
    [delegate mustDeselectWithout:self];
}

- (void)deselect{
    [button setBackgroundColor:[UIColor blueColor]];    
}

- (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 类实现协议 ProtocolCell。当我点击按钮时,我的委托调用方法 mustDeselectWithout 并取消选择数组中没有当前按钮的所有按钮。

#import <Foundation/Foundation.h>
@class CustomCell;

@protocol ProtocolCell <NSObject>
- (void)mustDeselectWithout:(CustomCell *)cell;
@end

这是我的实现。如果你能发表评论我会很高兴。因为我不知道这是否是我的问题的正确解决方案。谢谢!

I solved this problem as follows:

This my implementation of the UITableViewController class.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.delegate = self;
    [cells addObject:cell];

    // Configure the cell...

    return cell;
}

- (void)mustDeselectWithout:(CustomCell *)cell{
    for (CustomCell * currentCell in cells) {
        if(currentCell != cell){
            [currentCell deselect];
        }
    }
}

As you can see when I create cells I return custom cell in method cellForRowAtIndexPath.

This is class of the custom cell

.h

#import <UIKit/UIKit.h>
#import "ProtocolCell.h"

@interface CustomCell : UITableViewCell {
    UIButton * button;
}
@property(nonatomic, assign) id<ProtocolCell> delegate;
- (void)select;
- (void)deselect;
@end

and .m

#import "CustomCell.h"


@implementation CustomCell
@synthesize delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(5, 5, 25, 25)];
        [button setBackgroundColor:[UIColor blueColor]];
        [button addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
    }
    return self;
}

- (void)select{
    [button setBackgroundColor:[UIColor redColor]];
    [delegate mustDeselectWithout:self];
}

- (void)deselect{
    [button setBackgroundColor:[UIColor blueColor]];    
}

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

    // Configure the view for the selected state
}

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

@end

My class UITableViewController implements protocol ProtocolCell. When I tap on the button my delegate call the method mustDeselectWithout and deselect all button in my array without current button.

#import <Foundation/Foundation.h>
@class CustomCell;

@protocol ProtocolCell <NSObject>
- (void)mustDeselectWithout:(CustomCell *)cell;
@end

This is my implementation. If you could make comments I will be happy. Because I don know this is correct solution of the my problem or not. Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文