将IBOutlets连接到UITableViewCell原型 [英] Connecting IBOutlets to UITableViewCell prototype

查看:74
本文介绍了将IBOutlets连接到UITableViewCell原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义UITableViewCell原型创建一个UITable。
我的单元格包含UIImageViews,UILabels和UIButtons。



当我控制并从我的按钮拖动到我的班级界面时,它可以正常工作。但是,它不适用于网点。



当我在.h文件中创建IBOutlet时,如果我选择UITable而不是单元格,我只能连接,当然结果是应用程序损坏。



你们有任何想法如何解决这个问题吗?我不想仅为单元格使用自定义类。我真的很想坚持使用Storyboard和原型。



提前致谢

解决方案

使用带有标签的标签将完成工作,但绝不是一个好的做法...最好的方法是创建一个自定义的UITableViewCell类。



即,选择



新文件> Cocoa Touch> Objective C Class




创建它作为UITableViewCell
的子类现在你将得到.h和.m文件..



下一步是为此创建单元格视图



选择



新文件>用户界面>清空



现在用你的customcell类的同名创建它(比如说CustomCell)



现在您将有三个文件 CustomCell.h,CustomCell.m,CustomCell.xib



现在选择xib文件并在xib上添加UITableViewCell对象并设置其自定义类asCustomCell



查看下面的图片



此后,您可以将任何内容(UIImageView,UITextfield,UIButton)拖动到下面的视图,并将出口放到CustomClass上并管理操作使用委托方法..



如果你有imageView出口作为titleImage ..然后你可以通过在CellForRowAtIndex(TableView delgate方法)中创建单元格对象来设置图像。

  cell.titleImage = [UIImage ImageNamed:@goo.png]; 

现在我要说的另一件事是你必须在CustomCell中实现一个init方法。 m加载笔尖>>



它看起来像下面的代码。

   - (id)initWithDelegate:(id)parent reuseIdentifier:(NSString *)reuseIdentifier 
{

if(self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
{
self =(CustomCell *)[[[NSBundle mainBundle] loadNibNamed:@CustomCellowner:nil options:nil] lastObject];
}

self.backgroundColor = [UIColor clearColor];
self.backgroundView = NULL;
self.selectedBackgroundView = NULL;

//如果你想要任何委托方法,并且单元格有委托协议定义
self.delegate = parent;

//返回单元格
返回self;
}

现在,如果您使用单元格上的按钮,最好有代表



因此在按钮操作方法中,您可以调用委托方法(传递单元对象)并在ViewController中使用TableView实现委托



这里是示例





现在您可以使用您的单元格来填充UITableView ...而不是在CustomCell.xib中设置reuseIdentifier值(与设置CustomClass相同) )



让我们设置它,嗯还有什么customCell



所以在填充tableView时使用

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
NSString * cellIdentifier = @customCell;
CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[CustomCell alloc] initWithDelegate:self reuseIdentifier:cellIdentifier];

//设置单元格属性
cell.titleImage = [UIImage ImageNamed:@title.png];



返回单元格;
}

也不要忘记添加委托方法



  ViewController:UIViewController< CustomCellDelegate>在$ View $ @ $ $ $ $ $ p>然后在你的ViewController.m(实现文件)中实现它的主体



as

   - (void)cellButtonPressed:(CustomCell *)cell 
{
NSLog(@Pressed);
}

最好为您的单元格提供索引属性以处理您的表格选择取消选择方法



提供

  @property int index;您的CustomCell.h中的



和还



添加

  cell.index = indexPath.row; 

在你的tableView cellAtRow委托......



这看起来像一个很长的方法,但它安静有用且可读...



------- NB ---- ------:



如果您有任何对齐问题,只需通过实现

$ b $返回CustomCell高度
b

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{}

可能会发生....


I am creating a UITable with custom UITableViewCell prototype. My cell contains UIImageViews, UILabels and UIButtons.

When I control and drag from my buttons to my class' interface it works just fine. However, it does not work with outlets.

When I create the IBOutlet in the .h file, I can only connect if I select the UITable not the cell and of course the result is a broken app.

Do you guys have any idea how to solve this? I do not want to use a custom class just for the cell. I really would like to stick with Storyboard and prototypes.

Thanks in advance

解决方案

Using Labels with tag will get the job done but never a good practise ... the best way is to create a custom class of UITableViewCell.

ie, select

New File>Cocoa Touch >Objective C Class

and Create it as a subclass of UITableViewCell now you will get .h and .m files..

Next step is to create the view of the cell for this

select

New File> User Interface > Empty

and now create this with same name of your customcell class (lets say "CustomCell")

now you will have three files CustomCell.h,CustomCell.m,CustomCell.xib

now select the xib file and add UITableViewCell object on the xib and set its custom class as "CustomCell"

Look below pic

now after this you can drag any thing (UIImageView,UITextfield,UIButton) to the below view and give outlets onto the CustomClass and manage the actions using delegate methods..

if you have imageView outlet as titleImage ..then you can access the same by creating the cell object in CellForRowAtIndex (TableView delgate method) to set image.

cell.titleImage=[UIImage ImageNamed:@"goo.png"]; 

now one more thing i have to say is that you have to implement an init method also in CustomCell.m to load the nib>>

it will look like the below code.

    -(id)initWithDelegate:(id)parent reuseIdentifier:(NSString *)reuseIdentifier
    {

        if (self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
        {
            self=(CustomCell*)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil] lastObject];
        }

        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = NULL;
        self.selectedBackgroundView =NULL;

//If you want any delegate methods and if cell have delegate protocol defined
self.delegate=parent;

//return cell
    return self;
    }

Now it is better to have delegates if you are using buttons on your cell

so that in the button action method you can call delegate method (pass cell object) and implement the delegate in your ViewController with TableView

here is the example

now you can use your cell for UITableView to populate...and dont for get to set reuseIdentifier Value in CustomCell.xib (same as you set CustomClass)

lets set it namely ,hmm what else "customCell"

so while populating the tableView use

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier=@"customCell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
         cell= [[CustomCell alloc] initWithDelegate:self reuseIdentifier:cellIdentifier];

//set cell properties
   cell.titleImage=[UIImage ImageNamed:@"title.png"];



    return cell;
}

also dont forget to add delegate methods

give

ViewController:UIViewController<CustomCellDelegate>

on your ViewController.h file of ViewController

and then implement its body in your ViewController.m (implementation file)

as

-(void)cellButtonPressed:(CustomCell*)cell
{
NSLog(@"Pressed");
}

it is better to give index property to your cell to handle your table select deselect methods

provide

@property int index;

in your CustomCell.h

and also

add

cell.index=indexPath.row;

in your tableView cellAtRow delegate.....

This will look like a long method but its quiet useful and readable...

-------NB----------:

If you have any alignment issue just return the CustomCell height by implementing

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{}

it may occur....

这篇关于将IBOutlets连接到UITableViewCell原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆