iphone实现一个最简单的TableView

1. 打开xcode,依次点击菜单栏的File -> New Project -> Application -> View-based Application

 

2. Choose -> 输入project名称 SimpleTableView -> Save

 

3. 双击Resources组(这里不叫文件夹,叫Group组)展开该组。

 

4. 双击 SimpleTableViewViewController.xib,按下shift + command + L快捷键,显示出来了Library面板。

 

5. 当按下了shift + command + L时,焦点会在Library面板底部的搜索框内,直接输入Table,  

 

    把Table View拖到刚才打开的View窗口中,然后按下command + 2快捷键,

 

    把 dataSource、delegate拖到xib窗口中的 File's Owner中,按下command + S保存。

 

6. 然后双击Classes组,单击 SimpleTableViewViewController.h 文件,

 

    @interface SimpleTableViewViewController : UIViewController {

 

    把上面这行改成下面这行

@interface SimpleTableViewViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {

  然后加入成员变量m_data,是TableView待会要显示的数据。

 

    代码如下:

@interface SimpleTableViewViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {
	NSArray *m_data;
}
@property (nonatomic, retain) NSArray *m_data;
@end

 7. 单击 SimpleTableViewViewController.m 文件

 

    在 @implementation SimpleTableViewViewController 下面加如下一行代码

 

@synthesize m_data;  

 8. 把 SimpleTableViewViewController.m 文件中的 viewDidLoad 函数的注释去掉并加入以下代码

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSArray *arr = [[NSArray alloc] initWithObjects: @"桔子", @"雪梨", @"毛桃", @"李子", @"荔枝", @"柚子", @"芒果", @"菠萝", @"草莓", @"西瓜", nil];
	
	self.m_data = arr;
	[arr release];
    [super viewDidLoad];
}

 9. 最后在 SimpleTableViewViewController.m 文件最下面 @end 之前,加入以下代码

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [m_data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *TableViewDynamicLoadIdentifier = @"TableViewDynamicLoadIdentifier";
	
	UITableViewCell *pCell = [tableView dequeueReusableCellWithIdentifier:TableViewDynamicLoadIdentifier];
	if (pCell == nil)
	{
		pCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewDynamicLoadIdentifier] autorelease];
	}
	
	NSInteger nRow = [indexPath row];
	pCell.textLabel.text = [m_data objectAtIndex:nRow];
	
	return pCell;
}

 10. 编译并运行后的效果如下图如示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 QML 中实现点击 TableView 弹出一个界面,可以通过使用模态对话框(Modal Dialog)或者其他自定义的弹出界面来实现。下面是一个使用模态对话框的示例: 首先,创建一个自定义的弹出界面,比如一个叫做 PopupDialog 的组件: ```qml Rectangle { id: popupDialog width: 200 height: 150 color: "white" border.color: "black" radius: 5 // 弹出界面的内容 Text { text: "这是一个弹出界面" anchors.centerIn: parent } } ``` 然后,在 TableView 的 onClicked 信号处理函数中,控制 PopupDialog 的显示和隐藏: ```qml TableView { id: tableView // 表格内容设置... // 点击 TableView 的项时触发的信号处理函数 onItemClicked: { // 创建并显示模态对话框 var popup = Qt.createQmlObject('import QtQuick.Controls 2.15; PopupDialog {}', tableView); popup.parent = tableView; popup.x = (tableView.width - popup.width) / 2; popup.y = (tableView.height - popup.height) / 2; popup.open(); // 点击对话框外部时关闭对话框 popup.clickedOutside.connect(function() { popup.close(); popup.destroy(); }); } } ``` 在上述示例中,当点击 TableView 的项时,通过创建一个模态对话框 PopupDialog 来显示弹出界面。通过设置对话框的位置和大小,以及处理点击对话框外部时关闭对话框的逻辑,实现了点击 TableView 弹出一个界面的效果。 你可以根据自己的需要,调整弹出界面的样式和布局,以及对话框的显示和关闭逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值