C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – didSelectRowAtIndexPath一次选择多行大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个填充了27行的UITableView.我正在尝试更改所选单元格的accessoryType.我在didSELEctRowATindexPath:委托方法中这样做.

我面临的问题是,当选择一行并更改单元格的accessoryType时,该行的第十一行也会被修改.

我已经尝试打印[indexPath row]值,但它只显示选中的行而不是另一行.

我对这些东西感到很困惑;请帮帮我.

添加代码cellForRowATindexPath方法

UITableViewCell *cell;
if ([indexPath row] == 0) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
}
else {
    cell = [tableView dequeueReusableCellWithIdentifier:@"bcell"];
}

cell.SELEctionStyle = UITableViewCellSELEctionStyleNone;

if (cell == nil && [indexPath row] != 0) {
    cell = [[[UITableViewCell alloc] initWithStyle:
             UITableViewCellStyleSubtitle reusEIDentifier:@"bcell"] autorelease];
}
else if(cell == nil && [indexPath row] == 0){
    cell = [[[UITableViewCell alloc] initWithStyle:
             UITableViewCellStyleSubtitle reusEIDentifier:@"acell"] autorelease];
}

if ([cell.contentView subviews]){
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}
if ([indexPath row] == 0) {
    cell.textLabel.text = @"SELEct All";
    cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0f];
}
else {
    cell.textLabel.text = @"Some Text Here"
    cell.detailTextLabel.text = @"Another piece of text here"   
}
return cell;

我在做 ?因为行为在第11行之后重复,因此尝试为每第11行创建新对象.

我的didSELEctRowATindexPath方法代码

UITableViewCell *cell = [tableView cellForRowATindexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessorycheckmark) {
    if ([indexPath row] != 0) {
        NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell *tempCell = [tableView cellForRowATindexPath:tempIndex];
        tempCell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
}
else{
    cell.accessoryType = UITableViewCellAccessorycheckmark;
}
if ([indexPath row] == 0) {
    for (int i = 0; i < [datasource count]; i++) {
        NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:i+1 inSection:0];
        UITableViewCell *tempCell = [tableView cellForRowATindexPath:tempIndex];
        if (cell.accessoryType == UITableViewCellAccessorycheckmark) {
            tempCell.accessoryType = UITableViewCellAccessorycheckmark;
        }
        else{
            tempCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

请帮我多种选择或任何其他方式来解决多选问题.

提前致谢!!

解决方法

这是一种方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:CellIdentifier] autorelease];        
    }

    [cell.textLabel setText:[NSString StringWithFormat:@"Row %d",indexPath.row]];

    NSIndexPath* SELEction = [tableView indexPathForSELEctedRow];
    if (SELEction && SELEction.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessorycheckmark; 
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }

    // Configure the cell.
    return cell;
}

- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowATindexPath:indexPath].accessoryType = UITableViewCellAccessorycheckmark;
}

- (void)tableView:(UITableView *)tableView didDeSELEctRowATindexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowATindexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

请记住,表视图中的每个单元实际上都是重用的对象.如果每次调用cellForRowATindexPath时都没有设置附件类型,当新单元格滚动到屏幕上时,它们将具有相同的附件.

多选

对于多重选择,它有点复杂.

您的第一个选择:未记录的API

请注意,这仅在表格处于编辑模式时才有效.将每个单元格的编辑样式设置为未记录的UITableViewCellEdiTingStyleMultiSELEct.完成后,您可以通过UITableView的未记录成员获取表视图的选择:indexPathsForSELEctedRows.这应该返回所选单元格的数组.

您可以通过将其置于标头中来公开此功能

enum {
    UITableViewCellEdiTingStyleMultiSELEct = 3,};

@interface UITableView (undocumented)
- (NSArray *)indexPathsForSELEctedRows;
@end

然后为每个单元格设置编辑样式,如下所示:

- (UITableViewCellEdiTingStylE)tableView:(UITableView *)tableView ediTingStyleForRowATindexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEdiTingStyleMultiSELEct;
}

当表处于编辑模式时,您将在单元格上看到多选控件.

要查看其他未记录的API,您可以使用NR_842_11845@命令行实用程序,如下所示:

nm /Developer/Platforms/iPhoneOs.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit

您的第二个选择:自己管理选择

让您的UITableView子类包含一个数组,指示选择了哪些单元格.然后在cellForRowATindexPath中,使用该数组配置单元格的外观.你的didSELEctRowATindexPath方法应该是这样的

- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath
{
    if ([tableView indexPathIsSELEcted:indexPath]) {
        [tableView removeIndexPathFromSELEction:indexPath];
    } else {
        [tableView addIndexPathToSELEction:indexPath];
    }
    // update the cell's appearance somewhere here
    [tableView deSELEctRowATindexPath:indexPath animated:NO];
}

这假设您在UITableView子类中创建indexPathIsSELEcted,removeIndexPathFromSELEction和addIndexPathToSELEction方法.这些方法应该完全符合它们的名称所暗示的:添加,删除和检查数组中的索引路径.如果使用此选项,则不需要didDeSELEctRowATindexPath实现.

大佬总结

以上是大佬教程为你收集整理的objective-c – didSelectRowAtIndexPath一次选择多行全部内容,希望文章能够帮你解决objective-c – didSelectRowAtIndexPath一次选择多行所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。