如果我们不通过代码,标题的更新如何工作?

时间:2014-04-04 07:07:06

标签: ios objective-c uitableview

- (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];
    }

   int flag = appdelegate.flag;

    if (flag) {

        if (![cell.contentView viewWithTag:1122377]) {

            UIButton *btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];

            if (appDelegate.deviceTypeIpad) {
                btnEdit.frame = CGRectMake(tableView.frame.size.width - 150, 10, 26, 24);
            }
            else {
                btnEdit.frame = CGRectMake(tableView.frame.size.width - 120, 10, 26, 24);

            }
            btnEdit.tag = 1122377;
            NSString *title = [NSString stringWithFormat:@"%d",indexPath.row];
            [btnEdit setTitle:title forState:UIControlStateNormal];
            [btnEdit setImage:[UIImage imageNamed:@"edit_ma.png"] forState:UIControlStateNormal];
            [btnEdit addTarget:self action:@selector(editConfiguration:) forControlEvents:UIControlEventTouchUpInside];
           // btnEdit.tag = indexPath.row;

            [cell.contentView addSubview:btnEdit];
        }
        UIButton *tempBtn = (UIButton *)[cell.contentView viewWithTag:1122377];
        NSLog(@"%@ titleString ", tempBtn.titleLabel);
        UIButton *imgView = (UIButton *)[cell.contentView viewWithTag:112238];

        if (!imgView) {

            if (![cell.contentView viewWithTag:112238]) {
                UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
                if (appDelegate.deviceTypeIpad) {
                    btnDelete.frame = CGRectMake(tableView.frame.size.width - 100, 10, 26, 24);
                }
                else {
                    btnDelete.frame = CGRectMake(tableView.frame.size.width - 75, 10, 26, 24);
                }
                btnDelete.tag = 112238;
                NSString *title = [NSString stringWithFormat:@"%d",indexPath.row];
                [btnDelete setTitle:title forState:UIControlStateNormal];
                [btnDelete setImage:[UIImage imageNamed:@"trash_ma.png"] forState:UIControlStateNormal];
                [btnDelete addTarget:self action:@selector(deleteConfiguration:) forControlEvents:UIControlEventTouchUpInside];
                // btnDelete.tag = indexPath.row;

                [cell.contentView addSubview:btnDelete];

            }
        }
        NSLog(@"%@ titleString",imgView.titleLabel);
    }
    else {

        [[cell.contentView viewWithTag:1122377] removeFromSuperview];
        [[cell.contentView viewWithTag:112238] removeFromSuperview];

    }
}


-(void)deleteConfiguration:(id)sender
{

    updateIndex =  [[[(UIButton *)sender titleLabel] text] integerValue];

}

如果单元格中已存在“编辑”或“删除”按钮,则表示我在此方法中无效。

仍然如果我正在删除任何单元格,那么如何删除下一个单元格我得到正确的updateIndex,因为UITableView中的索引将在删除单个单元格时立即更改。请给出答案找不到原因..

另外请告诉我它是否正确添加单元格上的按钮或者总是如果我们需要向单元格添加按钮我们应该自定义单元格然后添加,即使我们需要添加一个按钮。

提前多多感谢

编辑1:逻辑上解决方案是:

- (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];
    }

    int flag = appdelegate.flag;


//   [[cell.contentView viewWithTag:1122377] removeFromSuperview];
//   [[cell.contentView viewWithTag:112238] removeFromSuperview];

    if (flag) {

        if (![cell.contentView viewWithTag:1122377]) {

            UIButton *btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];

            if (appDelegate.deviceTypeIpad) {
                btnEdit.frame = CGRectMake(tableView.frame.size.width - 150, 10, 26, 24);
            }
            else {
                btnEdit.frame = CGRectMake(tableView.frame.size.width - 120, 10, 26, 24);

            }
            btnEdit.tag = 1122377;
            NSString *title = [NSString stringWithFormat:@"%d",indexPath.row];
            [btnEdit setTitle:title forState:UIControlStateNormal];
            [btnEdit setImage:[UIImage imageNamed:@"edit_ma.png"] forState:UIControlStateNormal];
            [btnEdit addTarget:self action:@selector(editConfiguration:) forControlEvents:UIControlEventTouchUpInside];
            // btnEdit.tag = indexPath.row;

            [cell.contentView addSubview:btnEdit];
        }
        else {
            UIButton *btn = (UIButton *)[cell.contentView viewWithTag:1122377];
            NSString *title = [NSString stringWithFormat:@"%d",indexPath.row];
            [btn setTitle:title forState:UIControlStateNormal];
        }


        if (![cell.contentView viewWithTag:112238]) {
            UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
            if (appDelegate.deviceTypeIpad) {
                btnDelete.frame = CGRectMake(tableView.frame.size.width - 100, 10, 26, 24);
            }
            else {
                btnDelete.frame = CGRectMake(tableView.frame.size.width - 75, 10, 26, 24);
            }
            btnDelete.tag = 112238;
            NSString *title = [NSString stringWithFormat:@"%d",indexPath.row];
            [btnDelete setTitle:title forState:UIControlStateNormal];
            [btnDelete setImage:[UIImage imageNamed:@"trash_ma.png"] forState:UIControlStateNormal];
            [btnDelete addTarget:self action:@selector(deleteConfiguration:) forControlEvents:UIControlEventTouchUpInside];
            // btnDelete.tag = indexPath.row;

            [cell.contentView addSubview:btnDelete];

        }
        else {
            UIButton *btn = (UIButton *)[cell.contentView viewWithTag:112238];
            NSString *title = [NSString stringWithFormat:@"%d",indexPath.row];
            [btn setTitle:title forState:UIControlStateNormal];
        }

    }
    else {

        [[cell.contentView viewWithTag:1122377] removeFromSuperview];
        [[cell.contentView viewWithTag:112238] removeFromSuperview];

    }


    return cell;
}

我可以修复我之前的代码(其中更新按钮的标题)一旦我获得了它的错误,但我无法重现该错误。虽然之前的代码不正确,但它是如何工作的却是真正的问题。请花时间告诉它背后隐藏的东西。提前感谢很多..请告诉我,如果我不提供任何其他细节。< / p>

0 个答案:

没有答案