Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将IBOutlets连接到UITableViewCell原型_Ios_Uitableview_Xcode5 - Fatal编程技术网

Ios 将IBOutlets连接到UITableViewCell原型

Ios 将IBOutlets连接到UITableViewCell原型,ios,uitableview,xcode5,Ios,Uitableview,Xcode5,我正在使用自定义UITableViewCell原型创建一个UITable。 我的单元格包含UIImageView、UILabels和UIButtons 当我控制并从我的按钮拖动到我的类界面时,它工作得很好。但是,它不适用于插座 当我在.h文件中创建IBOutlet时,我只能在选择UITable而不是cell的情况下连接,当然结果是一个坏掉的应用程序 你们知道怎么解决这个问题吗?我不想只为单元格使用自定义类。我真的很想坚持故事板和原型 提前感谢您您必须为单元格创建一个自定义UITableViewC

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

当我控制并从我的按钮拖动到我的类界面时,它工作得很好。但是,它不适用于插座

当我在.h文件中创建IBOutlet时,我只能在选择UITable而不是cell的情况下连接,当然结果是一个坏掉的应用程序

你们知道怎么解决这个问题吗?我不想只为单元格使用自定义类。我真的很想坚持故事板和原型


提前感谢您

您必须为单元格创建一个自定义UITableViewCell类,然后仅通过cntrl+拖动来创建插座才有意义,否则还有一种更简单的方法。在UIImageView、UILabels等中使用99343、99345等标记(以避免重叠),并像这样访问它们

  UILabel *myLabelFromPrototypeCell = (UILabel*)[cell.contentView viewWithTag:99343];

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

ie,选择

新文件>可可触控>目标C类

及 将其创建为UITableViewCell的子类 现在您将获得.h和.m文件

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

挑选

新建文件>用户界面>清空

现在,使用与customcell类相同的名称创建它(比如“customcell”)

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

现在选择xib文件并在xib上添加UITableViewCell对象,并将其自定义类设置为“CustomCell”

看下面的图片

现在,您可以将任何内容(UIImageView、UIExtField、UIButton)拖到下面的视图中,并在CustomClass上提供出口,并使用委托方法管理操作

如果您将imageView出口设置为titleImage..,则可以通过在CellForRowatinex(TableView delgate方法)中创建单元格对象来设置图像来访问相同的出口

cell.titleImage=[UIImage ImageNamed:@"goo.png"]; 
现在我还要说的是,您必须在CustomCell.m中实现一个init方法来加载nib>>

它看起来像下面的代码

    -(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;
    }
现在,如果您使用的是单元格上的按钮,则最好有代理

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

这是一个例子

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

也就是说,嗯,还有什么“自定义单元格”

因此,在填充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];
       
//set cell properties
   cell.titleImage=[UIImage ImageNamed:@"title.png"];

    
    
    return cell;
}
另外,不要忘记添加委托方法

给予

这看起来像是一个很长的方法,但是它非常有用并且可读

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

另外,返回自定义单元格高度 实施

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

可能会出现……

请提供示例使用自定义类有何错误?没有错误。我想知道我是否可以像我这样连接一个动作(拖放)时那样做。但是,我将它们存储在属性中,以便在其他方法中使用它们。我问这个问题是因为我想在协议表中的单元格中设置我的所有视图会很混乱视图:cellforrowatinexpath谢谢你的全面回答。cell.index=indexPath.row您的意思是在cellforrowatinex中。。。正确的?如果我使用故事板和原型单元,我可以跳过init方法,或者我确实需要覆盖它?@Camus no dude。。如果使用此方法,则不需要在TableView上使用prototypecell(即拖放到TableView)。只需将表视图的单元格类型更改为动态原型。关于cell.index。。你是对的。。。如果有任何对齐问题,只需通过实现-(CGFloat)tableView:(UITableView*)tableView heightforrowatinexpath:(nsindepath*)indepath{}返回CustomCell height即可。这可能会发生……我知道了。我使用原型电池进行了测试,效果很好。我更喜欢这样,因为我不需要创建XIB。谢谢,在使用原型单元时,您的nib文件路径可能会更改!!但是使用xib是一个很好的实践…你知道…因为如果你想改变任何特性或添加任何。。。您可以轻松地在xib上进行编辑,而不是在故事板上进行编辑。这将使其更加拥挤…但这是您的选择,并且是根据需要进行的…实际上,我只需要更改我写入dequeueReusableCellWithIdentifier:@Raon的行
  -(void)cellButtonPressed:(CustomCell*)cell
    {
NSIndexPath *indexPathOfPressedCell = [self.tableView indexPathForCell:cell];
    NSLog(@"Pressed");
    }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{}