可编辑的UITableView,每个单元格上都有一个文本字段 [英] Editable UITableView with a textfield on each cell

查看:105
本文介绍了可编辑的UITableView,每个单元格上都有一个文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS世界的新手,我想知道如何使用自定义单元格制作 UITableView ,其外观和行为与您尝试配置时的单元格相似您的设备上有一些WiFi连接。 (你知道 UITableView ,其中包含带有 UITextField 的单元格,其中包含蓝色字体,您可以在其中设置IP地址和所有内容...)。

I am new to the iOS world and I want to know how to make a UITableView with custom cells that look and behave like the one you have when you try to configure some WiFi connexion on your device. (You know the UITableView with cells containing UITextFields with blue font where you set up the ip address and all that stuff... ).

推荐答案

要制作自定义单元格布局确实涉及一些编码,所以我希望不要吓到你。

To make a custom cell layout do involve a bit of coding, so I hope that dosen't frighten you.

首先要创建一个新的 UITableViewCell 子类。我们称之为 InLineEditTableViewCell 。您的界面 InLineEditTableViewCell.h 可能如下所示:

First thing is creating a new UITableViewCell subclass. Let's call it InLineEditTableViewCell. Your interface InLineEditTableViewCell.h could look something like this:

#import <UIKit/UIKit.h>

@interface InLineEditTableViewCell : UITableViewCell

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UITextField *propertyTextField;

@end

以及 InLineEditTableViewCell.m 可能如下所示:

#import "InLineEditTableViewCell.h"

@implementation InLineEditTableViewCell

@synthesize titleLabel=_titleLabel;
@synthesize propertyTextField=_propertyTextField;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Here you layout your self.titleLabel and self.propertyTextField as you want them, like they are in the WiFi settings.
    }
    return self;
}

- (void)dealloc
{
    [_titleLabel release], _titleLabel = nil;
    [_propertyTextField release], _propertyTextField = nil;
    [super dealloc];
}

@end

下一步是你设置 - 像往常一样在视图控制器中启动 UITableView 。执行此操作时,您必须实现 UITablesViewDataSource 协议方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 。在为此插入实现之前,请记住视图控制器中的 #importInLineEditTableViewCell。执行此操作后,实现如下:

Next thing is you set-up your UITableView as you normally would in your view controller. When doing this you have to implement the UITablesViewDataSource protocol method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. Before inserting your implementation for this, remember to #import "InLineEditTableViewCell" in your view controller. After doing this the implementation is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InLineEditTableViewCell *cell = (InLineEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"your-static-cell-identifier"];

    if (!cell) {
        cell = [[[InLineEditTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"your-static-cell-identifier"] autorelease];
    }

    // Setup your custom cell as your wish
    cell.titleLabel.text = @"Your title text";
}

就是这样!您现在在 UITableView 中有自定义单元格。

That's it! You now have custom cells in your UITableView.

祝你好运!

这篇关于可编辑的UITableView,每个单元格上都有一个文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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