我的代码与其他代码有点不同,但它可以工作。
我是应用程序编码新手,但我想添加一些 这些分为几个部分:
所以他们中的一些人有自己的小组,每个小组都有一个小标题。
但是我的代码如下所示:
我不知道要插入什么才能正确执行。
(该图片的下半部分是详细 View 中的图片,当您从表格 View 中选择某些内容时,该图片会显示在详细 View 中。)
(我知道 Xcode 在我的代码中显示错误,但它仍然有效。)
谁能帮帮我?
最佳答案
你必须实现一些 UITableView 方法和委托(delegate)方法(当然还要将你的类设置为该类的委托(delegate)):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Here you must return the number of sectiosn you want
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Here, for each section, you must return the number of rows it will contain
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//For each section, you must return here it's label
if(section == 0) return @"header 1";
.....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.text = // some to display in the cell represented by indexPath.section and indexPath.row;
return cell;
}
这样,您就可以根据需要排列数据:每个部分一个数组,一个带有子数组的大数组,...根据您的需要。只要您可以从上面的方法返回想要的值,Antthing 就可以了。
关于ios - 在 Xcode 的分组 TableView 中添加部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056562/