UITableViewCell 中的 iOS 动态按钮标记未检测到表中的正确行

Posted

技术标签:

【中文标题】UITableViewCell 中的 iOS 动态按钮标记未检测到表中的正确行【英文标题】:iOS Dynamic Button tagging in UITableViewCell does not detect correct row in table 【发布时间】:2012-07-29 10:23:07 【问题描述】:

我现在遇到了一个大问题,因为我的更新按钮根本没有检测到由于回收而被选中的行!尝试标记按钮,但它也没有检测到正确的按钮。请帮忙!我已经坚持了好几天了。如果没有,请随意推荐除标记之外的其他更好的解决方案。我通过 NSDictionary 解析部分和行,如下所示:

"15/08/2012" =     (
            
        BIBNo = 290408;
        date = "15/08/2012";
        itemSequence = 000010;
        name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
        noOfRenewal = 3;
        number = 000290408;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    
);
"16/08/2012" =     (
            
        BIBNo = 187883;
        date = "16/08/2012";
        itemSequence = 000010;
        name = "Positive thinking / Susan Quilliam.";
        noOfRenewal = 3;
        number = 000187899;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    ,
            
        BIBNo = 161816;
        date = "16/08/2012";
        itemSequence = 000010;
        name = "Malaysian Q & As / compiled by Survey & Interview Department ; illustrations by Exodus.";
        noOfRenewal = 1;
        number = 000161817;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    
);
"17/08/2012" =     (
            
        BIBNo = 187882;
        date = "17/08/2012";
        itemSequence = 000010;
        name = "Increasing confidence / Philippa Davies.";
        noOfRenewal = 1;
        number = 000187907;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    ,
            
        BIBNo = 244441;
        date = "17/08/2012";
        itemSequence = 000010;
        name = "Coach : lessons on the game of life / Michael Lewis.";
        noOfRenewal = 2;
        number = 000244441;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    ,
            
        BIBNo = 291054;
        date = "17/08/2012";
        itemSequence = 000010;
        name = "iPhone application development for ios 4 / Duncan Campbell.";
        noOfRenewal = 3;
        number = 000291054;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    
);

我的cellForRowAtIndexPath代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"Cell";
UserCustomCell *cell = (UserCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);

if (cell == nil) 
    [[NSBundle mainBundle] loadNibNamed:@"UserCustomCell" owner:self options:nil];
    cell = userCustomCell;
    self.userCustomCell = nil;


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
    cell.renewButton.frame = CGRectMake(600, 14, 68, 24);

[cell.renewButton useBlackActionSheetStyle];

dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar

for (NSDictionary *rawItem in myArray) 
    NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key

    NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date

    if (!section)  // If there is no section then create one and add it to the dataSource
        section = [[NSMutableArray alloc] init];
        [dataSource setObject:section forKey:date];
    
    [section addObject:rawItem]; // add your object

self.dataSource = dataSource;
NSLog(@"Data Source Dictionary: %@", dataSource); 

NSArray *sections =[[dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *sectionTitle = [sections objectAtIndex:indexPath.section];

NSArray *items = [dataSource objectForKey:sectionTitle];

NSDictionary *dict = [items objectAtIndex:indexPath.row];
cell.bookTitle.text = [dict objectForKey:@"name"];
cell.detail.text = [NSString stringWithFormat:@"Due Date: %@ Due Time: %@", 
                    [dict objectForKey:@"date"], [dict objectForKey:@"time"]];


cell.renewButton.tag = indexPath.row;
return cell;   

现在的样子:

非常感谢!

【问题讨论】:

不要在这个方法中创建你的dataSource;初始化并在viewDidLoadviewWillAppear...中填充一次...只要您有可用的数据。除此之外,处理“更新”按钮的代码是什么? @Phillip 它将触发检查互联网连接的方法,并检查 button.tag 的续订次数(因为最大续订次数为 3 次) NSString *noRenewal = [[ myArray objectAtIndex:button.tag] objectForKey:@"noOfRenewal"]; 您应该使用相同的集合来查找用于填充单元格的更新。因此,我再次建议您创建一次dataSource,然后使用部分/行从其中获取信息,而不是myArray。否则,您的标记值需要是数组的索引,而不是行。 【参考方案1】:

我已经通过在我的数组中添加索引解决了这个问题,这是由@Phillip 推荐的,看起来像这样:

for (int a = 0; a < myArray.count; a++)

    NSMutableDictionary *mDict = [myArray objectAtIndex:a];
    NSNumber *myIndex = [NSNumber numberWithInt:a];
    [mDict setValue:myIndex forKey:@"index"];
    NSLog(@"added index: %@", myArray);

self.myArray = myArray;

在我在按钮中设置标签之前,它会检测到正确的项目。

 NSString *index = [dict objectForKey:@"index"];
NSInteger index_tag = (@"%d", [index intValue]);
NSLog(@"%d", index_tag);
cell.renewButton.tag = index_tag;

希望它能帮助和我有同样问题的人:)

【讨论】:

以上是关于UITableViewCell 中的 iOS 动态按钮标记未检测到表中的正确行的主要内容,如果未能解决你的问题,请参考以下文章

iOS 8 UITableViewCell 与 UIImageView 动态高度

IOS:具有自定义 uitableviewcell 的动态高度

在 UITableViewCell ios 中制作动态 UILabel 高度/宽度?

iOS 中包含 UIWebView 的单元格的动态 UITableViewCell 高度

iOS Swift 如何更改或更新自定义 UITableViewCell 中的 UILabel 属性

iOS 7 和 iOS 8 中动态 UITableViewCell 的高度是不可能的