iOS UITableViewCells行回收和标记不工作 [英] iOS UITableViewCells rows recycled and tag not working

查看:119
本文介绍了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];

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

}

但它不一切工作。将是sooooo感谢任何建议:)谢谢!!

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

P.S:我的xcode副本不更新,只有版本4。看到有些人提到在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天全站免登陆