ios - 从数组设置自定义UITableViewCell的文本

标签 ios cocoa-touch uitableview

目前,我使用以下方法填充单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // builds the settingsView tableView
    if (menuList) {
        static NSString *cellIdentifier = @"coachingCell";

        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        if (!contentLoaded) {
            numberOfCells = 0;
        }
        if (contentLoaded) {
            [cell.textLabel setText:[names objectAtIndex:numberOfCells]];
            NSLog(@"Entries: %@", [names objectAtIndex:numberOfCells]);
            if (numberOfCells == [names count]) {
                numberOfCells = 0;
            }
        numberOfCells ++;
        }
        //add a switch
        coachingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = coachingSwitch;
        [coachingSwitch setOn:YES];

        return cell;
    }
    ...

但是,如果将所有内容滚动到底部,然后将一个对象滚动到顶部,则会出现错误。那是因为我在一个计数器上方填充了单元格:numberOfCells

现在我的问题是:我该如何解决这个问题,以便实际上迭代正确的indexPath.sectionindexPath.row的数组?
names数组包含:
2012-12-09 16:34:22.373 Test[18817:c07] Entries: Mein Bauchumfang
2012-12-09 16:34:22.375 Test[18817:c07] Entries: Mein Gewicht
2012-12-09 16:34:22.375 Test[18817:c07] Entries: Mein Entspannungsniveau
2012-12-09 16:34:22.376 Test[18817:c07] Entries: Häufigkeit der Entspannungsmomente
2012-12-09 16:34:22.377 Test[18817:c07] Entries: Rauchverhalten
2012-12-09 16:34:22.378 Test[18817:c07] Entries: Verlangen nach einer Zigarette
2012-12-09 16:34:22.378 Test[18817:c07] Entries: Systolischer Wert
2012-12-09 16:34:22.379 Test[18817:c07] Entries: Diastolischer Wert
2012-12-09 16:34:23.713 Test[18817:c07] Entries: Meine bewusste Ernährung mit Diabetes
2012-12-09 16:34:23.964 Test[18817:c07] Entries: Meine Blutzucker-Werte
2012-12-09 16:34:25.384 Test[18817:c07] Entries: Mein Befinden
2012-12-09 16:34:25.584 Test[18817:c07] Entries: Meine Aktivitäten

这是我也可以使用的多维数组:
2012-12-09 17:23:06.891 stuff[19161:c07] ArrayList: (
    {
    "Bauchumfang & Gewicht" =         (
        "Mein Bauchumfang",
        "Mein Gewicht"
    );
    "Blutdruck & Puls" =         (
        "Systolischer Wert",
        "Diastolischer Wert"
    );
    "Blutzucker & Diabetes" =         (
        "Meine bewusste Ern\U00e4hrung mit Diabetes",
        "Meine Blutzucker-Werte"
    );
    "Mein Rauchverhalten" =         (
        Rauchverhalten,
        "Verlangen nach einer Zigarette"
    );
    "Meine Stimmung" =         (
        "Mein Befinden",
        "Meine Aktivit\U00e4ten"
    );
    "R\U00fccken & Bewegung" =         (
        "Umsetzung meiner R\U00fccken\U00fcbungen",
        "Das Befinden meines R\U00fcckens",
        "Meine sportlichen Aktivit\U00e4ten"
    );
    Schlafrhythmus =         (
        "Meine Schlafqualit\U00e4t",
        "Mein Energieniveau"
    );
    "Schrittz\U00e4hler" =         (
        "Meine Schritte"
    );
    "Stress & Entspannung" =         (
        "Mein Entspannungsniveau",
        "H\U00e4ufigkeit der Entspannungsmomente"
    );
}
)

最佳答案

为什么不只使用indexPath.row而不是numberOfCells:

[cell.textLabel setText:[names objectAtIndex:indexPath.row]];

编辑:

您可以使用以下方法获取合适的数组索引:
int rowsUntilHere=indexPath.row;
 for (int i=0;i<indexPath.section;i++){
   rowsUntilHere+=[tableView numberOfRowsInSection:i];
   //rowsUntilNow++; //If the array contains the section titles too
 }

 [cell.textLabel setText:[names objectAtIndex:rowsUntilHere]];

希望这可以帮助

霍夫·达斯·希尔夫特

关于ios - 从数组设置自定义UITableViewCell的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788976/

相关文章:

iOS - 新闻源结构 : does Instagram use TableViews inside of TableCells?

ios - UICollectionViewCell 远程图像在滚动时消失

ios - 画外音读取 tableview 中的所有单元格

iphone - 如何判断按下了哪个 MKPinAnnotation?

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

iphone - 使用自定义节标题将行添加到 tableView

ios - 从 iOS 上的 flutter 应用程序打开 youtube 应用程序

c++ - Xcode 7 对包含文件的处理已更改

ios - 如何将 NSData/除图像/URL 之外的任何信息传递到 iOS8 中的 Action Extensions

objective-c - 动画没有通过另一个 View Controller 执行