ios - 如何以编程方式设置划分为多个部分的静态表中的单元格

标签 ios objective-c uitableview

我基本上是在我的应用程序上创建一个设置 View ,并为其使用静态表。因此,我将表格分为 3 个部分,每个部分有一个单元格。不以编程方式我可以轻松地标记每个单元格并且它可以工作,但以编程方式我无法初始化每个单元格。我只能初始化在 3 个部分中重复的第一个单元格。我想要一种方法来初始化每个部分的单元格,但我找不到方法或方法来做到这一点。我的 TableView 也有reuseIdentifiers,但似乎UITableViewController 无法识别它。

这就是我到目前为止所做的。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EditProfile";
    //UITableViewCell *cell = nil; //[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // More initializations if needed.
    }
    //Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
    cell.textLabel.text = @"Hey";
    //return cell;

    return cell;
}

但我想要的是第一部分中的单元格被标记为:编辑个人资料,第二部分:邀请,第三部分:注销

最佳答案

您必须为如何处理表格 View 中的每个不同部分制定一个条件。

if (indexPath.section == 0) {
    cell.textLabel.text = @"Edit Profile";
}else if (indexPath.section == 1) {
    cell.textLabel.text = @"Invite";
}else if (indexPath.section == 2) {
    cell.textLabel.text = @"Log out";
}......

关于ios - 如何以编程方式设置划分为多个部分的静态表中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18645805/

相关文章:

ios - Flutter Http 请求未在 iOS 上执行

ios - 将 ViewController 与 swift 中的类型进行比较

ios - 从数组中删除 - fatal error : Index out of range - SwiftUI Binding

ios - 如何仅使用代码将按钮正确添加到场景中?

iphone - UITableView 中的错误

ios - 为多个控件事件 UIButton 添加选择器

ios - Objective-C:具有一种模型和多种 View Controller 的 MVC

ios - 如何停止 UITextField 显示字符的安全文本

ios - 不使用 TableViewController 调用 didSelectRowAtIndexPath

ios - 我有一个分组的 UITableView,我需要让它看起来像一个普通的 UITableView