iOS UITableViewCells 行被回收并且标签不起作用 [英] iOS UITableViewCells rows recycled and tag not working

查看:15
本文介绍了iOS UITableViewCells 行被回收并且标签不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在为单元格中的按钮设置标签方面寻求一些帮助.这是一个链接到我之前发布的问题:iOS 使用 NSDictionary 将数据加载到节和行中

I would like to seek some help in setting tag for buttons in cells. This is a question with a link to the previous I posted : iOS Using NSDictionary to load data into section and rows

但是,虽然我现在可以动态传递数据,但我在每一行上的续订按钮似乎无法获取数据,并且只会在选择该部分中的任何行时检测到每个部分的相同书名.

However, though I could pass in data dynamically now, my renewal button on each of the rows could not seem to get the data and would only detect the same book title of each section when any of the rows in the section is selected.

根据我目前所阅读的内容,这是因为按钮正在被回收,因此无法检测到正确选择了哪本书.我试过设置标签:

Based on what I've read so far, it's because the button is being recycled hence, unable to detect which book is being selected properly. I've tried to set tag:

cell.renewButton.tag = indexPath.row;

我的代码现在的样子:

- (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;
    cell.renewButton.tag = indexPath.row;
}

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];//这需要是一个 ivar

//########## EDIT STARTS HERE 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;   

}

但它根本不起作用.非常感谢任何建议:) 谢谢!!

but it doesn't work at all. Would be sooooo grateful for any suggestions :) Thank you!!

P.S:我的 xcode 副本没有更新,只更新到 version4.看到有人提到在 DataModel 中存储标签状态,但它仅在较新版本中可用.:)

P.S: My copy of xcode is not updated, only till version4. Saw some people mentioning storing of tag status in DataModel but it's only available in newer versions. :)

推荐答案

我看不到你的 cell.renewButton 被分配了一个选择器方法(应该在点击按钮时触发的方法).

I could not see your cell.renewButton being assigned a selector method (the method that should be triggered on tapping the button).

[cell.renewButton addTarget:self action:@selector(renewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

另外,我会指定一个带有偏移量的标记号,因为标记为 0 几乎就像根本没有标记一样.tableView 的第一行将给出 indexPath.row = 0.

Also, I would specify a tag number with an offset, as tag of 0 is almost like not tagging at all. First row of tableView will give indexPath.row = 0.

在您的代码之上,

#define OFFSET 100 /* Or any number greater than 0 */

在 cellForRowAtIndexPath 中,

In cellForRowAtIndexPath,

...
[cell.renewButton addTarget:self action:@selector(renewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cell.renewbutton.tag = indexPath.row + OFFSET;
...

在renewButtonPressed方法中,

In the renewButtonPressed method,

-(void)renewButtonPressed:(id)sender
{
    tappedNum = [sender tag] - OFFSET;
    /* do your stuff */
}

tappedNum 将为您提供点击按钮的行,从 0 开始.

tappedNum will give you the row that the button is tapped, starting with 0.

这篇关于iOS UITableViewCells 行被回收并且标签不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆