Objective-c TableView实时刷新自定义单元格

时间:2015-12-01 17:05:57

标签: ios objective-c json uitableview

您好我有自定义单元格的tableview,我想在下面实时刷新我的代码。

https://c9users.io:8081/_user_content/authorize?redirect=http%3A%2F%2Fproject-user.c9users.io%2F

代码工作成功我只需要实时刷新tableview行。我仍然工作大约8-10个小时,但我没有解决它。

不要刷新每个单元格

(我的错误:在添加新行时在错误的行上显示红色背景)

       @interface ViewController () <UITableViewDataSource, UITableViewDelegate>

        @property (nonatomic, strong) NSMutableArray *jsonArray;
        @property (nonatomic, strong) NSMutableArray *array1;
        @property (nonatomic, strong) NSDictionary *entryDict;

        @end


        - (void)viewDidLoad {
            [super viewDidLoad];

         _tableView.delegate = self;
            [self refresh];
        }

        - (void)refresh
        {
            NSString *urlduzenli = [NSString stringWithFormat:@"http://bla.com/server/table.php?OrderId=%@",suankioturumid];
            NSURL * url = [NSURL URLWithString:urlduzenli];

            NSURLRequest * urlReq = [NSURLRequest requestWithURL:url];

            NSError * error;

            NSData * data = [NSURLConnection sendSynchronousRequest:urlReq returningResponse:nil error:&error];

            if (!error) {
                NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

                _jsonArray = [jsonDict objectForKey:@"orders"];

                _array1 =[[NSMutableArray alloc]init];

            }else{

            }
   [self.tableView reloadData];
            }

        -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

            return _jsonArray.count;
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *cellid=@"CustomCell";
            CustomTableViewCell *cell=(CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid];;

            if(cell==nil)
            {
                cell=[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];

            }

           _entryDict= _jsonArray[indexPath.row];
            NSString *countryName = _entryDict[@"order"];

            NSMutableSet *seenCharacters = [NSMutableSet set];
            NSMutableString *resultf = [NSMutableString string];

              cell.nameLabel.text= countryName;


    if([cell.nameLabel.text isEqualToString:@"1"]){

         cell.nameLabel.backgroundColor = [UIColor redColor];

    }else{

           }

            return cell;
        }

感谢您的帮助!谢谢

1 个答案:

答案 0 :(得分:0)

从我们在评论中的讨论中得出解决问题的一些选择。

1)您需要添加UITableView Delegate,其中显示CREATE OR REPLACE FUNCTION getIncomingAreaMovements(integer) RETURNS table (id_part_movement integer , id_part integer, id_area_origin integer , id_area_destination integer , id_user_sender integer, id_user_receiver integer, id_user_cancel integer, id_part_order integer, id_area_final_destination integer, cause character varying(50), description character varying(255), start_date timestamp, end_date timestamp, cancelation_cause character varying(255), canceled boolean, rejected boolean, id_block_movement integer) AS $$ DECLARE BEGIN return query select pm.* From part_movement pm where pm.id_area_final_destination = $1; END; $$ LANGUAGE plpgsql; ,然后在viewDidLoad中设置<UITableViewDataSource, UITableViewDelegate>

2)您遇到的新问题最可能在于您未在tableView.delegate = self;更改值的位置发布的代码。我会为每一行的_jsonArray值做一个NSLog,以确保行号对应_jsonArray中的正确项。尝试在if语句的else部分设置_jsonArray