IOS Tableview选择行不起作用

时间:2016-04-29 04:54:33

标签: ios objective-c uitableview

我试图在所选行中添加复选标记,并在再次选中时将其删除,但每次选择一行时,复选标记只会转到第一个单元格并在此之后向下移动(即我点击任何单元格,然后单元格0有一个复选标记,我点击任何其他单元格,单元格1有一个复选标记等...)。

这是我的didselectrowatindexpath和cellforrowatindexpath方法:

更新

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"preflabel"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@", data[indexPath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];

    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}

2 个答案:

答案 0 :(得分:3)

你有[tableView reloadData]这会清除你tableview的固有状态。

  

重新加载表视图会清除当前状态,包括当前选择。但是,如果显式调用reloadData,它将清除此状态,并且对layoutSubviews的任何后续直接或间接调用都不会触发重新加载。

了解更多信息see.

<强>更新

您正在didSelectRowAtIndexPath和cellForRowAtIndexPath中创建tableviewcells。你为什么做这个?这将覆盖所选的单元格。

package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;

import views.html.*;

import models.*;

/**
 * Manage a database of Providers
 */
public class Application extends Controller {

    /**
     * This result directly redirect to application home.
     */
    public static Result GO_HOME = redirect(
        routes.Application.list(0, "fullname", "asc", "")
    );

    /**
     * Handle default path requests, redirect to computers list
     */
    public static Result index() {
        return GO_HOME;
    }

    /**
     * Display the paginated list of Providers.
     * 
     * @param page Current page number (starts from 0)
     * @param sortBy Column to be sorted
     * @param order Sort order (either asc or desc)
     * @param filter Filter applied on computer names
     */
    public static Result list(int page, String sortBy, String order, String filter) {
        return ok(
            list.render( 
                Provider.page(page, 10, sortBy, order, filter),
                sortBy, order, filter
            )
        );
    }

    /**
     * Display the 'edit form' of a existing Computer.
     *
     * @param id Id of the computer to edit
     * // TODO : Fix wiring and button actions 
     */
    public static Result edit(Long id) {
        Form<Provider> computerForm = form(Provider.class).fill(
            Provider.find.byId(id)
        );
        return ok(
            editForm.render(id, computerForm)
        );
    }

    /**
     * Handle the 'edit form' submission 
     *
     * @param id Id of the computer to edit
     */
    public static Result update(Long id) {
        Form<Provider> computerForm = form(Provider.class).bindFromRequest();
        if(computerForm.hasErrors()) {
            return badRequest(editForm.render(id, computerForm));
        }
        computerForm.get().update(id);
        flash("success", "Computer " + computerForm.get().fullname + " has been updated");
        return GO_HOME;
    }

    /**
     * Display the 'new Provider form'.
     */
    public static Result create() {
        Form<Provider> computerForm = form(Provider.class);
        return ok(
            createForm.render(computerForm)
        );
    }

    /**
     * Handle the 'new Provider form' submission 
     */
    public static Result save() {
        Form<Provider> computerForm = form(Provider.class).bindFromRequest();
        if(computerForm.hasErrors()) {
            return badRequest(createForm.render(computerForm));
        }
        computerForm.get().save();
        flash("success", "Provider " + computerForm.get().fullname + " has been created");
        return GO_HOME;
    }

}

答案 1 :(得分:1)

你不应该在select方法中通过deque方法获取单元格,因为你得到的单元格不是你触摸的单元格,使用像取消选择方法一样的cellForCellAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}