【问题标题】:How to Set a tag Value in UITAbleView Cell and Pass It into URL in iOS?如何在 UITTableView 单元格中设置标签值并将其传递到 iOS 中的 URL?
【发布时间】:2015-02-26 07:08:30
【问题描述】:

我是 iOS 开发的新手,我制作了一个包含 UITableView 的应用程序我像一样编码但总是设置标签等于1我想要如果选择了单元格然后标签是1否则标签等于0请给我解决方案。

这里是我的代码

viewDidLoad

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]];

SelectedRows 是NSMutableArray

-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
    cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];

NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.tag=1;
    intValue=cell.tag;
    NSLog(@"Tag %d",intValue);
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.tag=0;
    NSLog(@"Tag %d",cell.tag);
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCustumCell *cell = (CategoryCustumCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *selectedCell= cell.categoryLabel.text;
NSLog(@"Selected Cell Value %@",selectedCell);

NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    [SelectedRows removeObject:obj];
    [tableView reloadData];
}else{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    [SelectedRows addObject:obj];
    [tableView reloadData];
}

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:SelectedRows forKey:@"SelectedRows"];
[userDef synchronize];
}

并且我将这个标记值传递到 Url Like as

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/insert_gcm.php?gcm_id=%@& category1=%d&category2=%d&category3=%d&category4=%d&category5 =%d&category6=%d&category7 =%d&category8=%d& category9=%d",uniqueIdentifier,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue]];

在这里我想要标记值1 如果单元格被选中,标记值0 如果单元格未被选中请给我解决方案

【问题讨论】:

  • 你为什么使用indexPath.section?使用indexPath.row需要在当前实现中做很多改动。
  • @MidhunMP 这里我的 Tableview 包含部分所以我写了索引 path.Section。 indexpath.sectionindex path.row 对我来说无关紧要,我只想标记值。
  • 一个部分有多少行?
  • @MidhunMP 每个部分只有一行。
  • 你把这个 NSURL *url=... 行放在哪里,你是如何计算 intValue 的?

标签: ios objective-c uitableview tags


【解决方案1】:

问题是您在 cellForRowAtIndexPath: 方法中将 intValue 设置为 1:

if ([SelectedRows containsObject:obj])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.tag=1;
    intValue=cell.tag;
    NSLog(@"Tag %d",intValue);
}

这就是为什么intValue 总是等于 1。

并且解决方案不是在cellForRowAtIndexPath: 的另一个条件中将intValue 设置为等于0。尽管您正确地将选定单元格的标签设置为 1 并将未选定单元格的标签设置为 0,但您不应在此方法中分配 intValue 变量。相反,您应该根据当前标签设置intValue。例如,如果每一行都有一个保存按钮,您需要将该按钮的标签设置为 0 或 1,然后利用按钮按下方法中的参数将 intValue 设置为等于 sender.tag

编辑:正如在形成 url 时使用的 cmets "intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValu‌​e" 中明确表示的,实际上应该是不同的数字,每个数字代表不同的单元格标签。但是通过像这样反复使用intValue,您实际上使用了9次相同的确切值。您实际上需要使用 9 个不同的值来执行您想要执行的操作。

您可以在cellForRowAtIndexPath: 中动态创建包含这 9 个值的数组。

viewDidLoad 中,您可以初始化一个类NSMutableArray 变量,其中正好有9 个索引都包含0:

self.tagArray = [[NSMutableArray alloc] init];
for (int i = 0 ; i < 9 ; i ++) {
    [self.tagArray addObject:[NSNumber numberWithInt:0]];
}

然后更新cellForRowAtIndexPath:中的那些索引:

-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...

    NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
    if ([SelectedRows containsObject:obj])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.tag=1;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.tag=0;
    }

    [self.tagArray replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInteger:cell.tag]];
    return cell;
}

然后在执行保存期间,使用存储在标签数组中的标签:

- (void)performSave {

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/insert_gcm.php?gcm_id=%@&category1=%@&category2=%@&category3=%@&category4=%@&category5=%@&category6=%@&category7=%@&category8=%@&category9=%@", uniqueIdentifier, self.tagArray[0], self.tagArray[1], self.tagArray[2], self.tagArray[3], self.tagArray[4], self.tagArray[5], self.tagArray[6], self.tagArray[7], self.tagArray[8]]];    
}

【讨论】:

  • 我知道了,但是当我选择所有单元格时,它为七个单元格设置标签1,因为我的屏幕只包含 7 个单元格,并且想要显示第 8 个和第 9 个单元格的表格视图想要滚动。跨度>
  • @iOSDeveloper 但是,如果您从不滚动到单元格 8 和 9,则永远无法选择它们...如果您滚动到它们并选择它们,然后向上滚动,则应为那些存储数字 1行。
  • 苏格兰人在这里我想要我的cell.accessoryType CheckMark 默认情况下我在我的cellForRowAtIndexPath 方法中写我写为cell.accessoryType = UITableViewCellAccessoryCheckmark 但它对我不起作用请给我解决方案。
  • @iOSDeveloper 哪个部分特别不适合您?如果您希望默认选中您的单元格,则必须在重新加载表格之前将部分编号存储在 SelectedRows 数组中...您这样做了吗?
【解决方案2】:

您错误地假设您维护状态的单元格将保持与相关行相关联(即,第 1 行的单元格将保持为第 1 行的单元格)。这是完全不可靠的,因为单元会根据系统确定的标准重复使用。

您需要在其他数据结构中维护您希望的状态,或者更有可能利用 tableView 知道其选择状态并且您无需重新发明特定轮子这一事实:How can you get the selected rows from a UITableView?

当您需要从所选行的集合中构建一个 URL 时,询问 tableView,将 indexPaths 转换为您需要的任何信息,然后继续。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多